Give an example how you can write a VIEW in Django?


Views are Django functions that take a request and return a response.  To write a view in Django we take a simple example of “Guru99_home” which uses the template Guru99_home.html and uses the date-time module to tell us what the time is whenever the page is refreshed.  The file we required to edit is called, and it will be inside mysite/myapp/

Copy the below code into it and save the file

       from datatime import datetime

      from django.shortcuts import render

     def home (request):

              return render(request, ‘Guru99_home.html’, {‘right_now’: datetime.utcnow()})

Once you have determined the VIEW, you can uncomment this line in

# url ( r ‘^$’ , ‘mysite.myapp.views.home’ , name ‘Guru99’),

The last step will reload your web app so that the changes are noticed by the web server.


© 2017