Question:

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

Answer:

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 view.py, 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 urls.py

# 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.


Keywords:

© 2017 QuizBucket.org