Write a few useful views
Each view is responsible for doing two things, returning a HttpResponse object that contains the contents of the requested page, or throwing an exception, such as Http404. What the rest of the view does depends on you.
Your view can read records from the database, or not read them. You can use a template system such as a Django-brought or a third-party Python templating system, or not. You can generate a PDF file in real time, output XML, and create a ZIP archive package. Everything you want to do can be done using any Python class library you want to use.
For convenience, we use the Django-based database API, which allows us to simply look at the index () view, which shows the latest 5 comma-delimited questions sorted by publication time.
From django.http import HttpResponse
From Polls.models import Poll
def index (Request):
Latest_poll_list = Poll.objects.order_by ('-pub_date ' [: 5])
Output = ', '. Join ([p.question for P in Latest_poll_list])
return HttpResponse (Output)
There is a problem here, the design of the page is hard coded in the view. If you want to change the appearance of the page, you will have to modify the code for this view. So we use the Django template system to create a view and page separation design by creating a template that can be used by the views.
First, create a folder with the name templates in your polls folder. Django will look for templates from templates.
The Django Template_loaders configuration contains a callable list that knows how to import templates from various sources. One of the default loaders is django.template.loaders.app_drectories. Loader, this loader looks for "templates" folders from subdirectories of each app installed in Intalled_apps, which is why Django knows how to find polls templates folders, even if we don't modify Template_ DIRS
I can put all the templates in a large Templates folder, and this will work just as well. But these templates belong to the polls app, so unlike the admin app we created earlier templates, we'll put this template in the app's templates
In the Templates folder that we just created, create another folder polls, under which Index.html is created under the polls folder. polls/templates/polls/index.html. Because the App_directories template loader works, you can reference it in Django in a polls/index.html way.
Template namespaces:
Now we can put our template directly into polls/templates instead of creating another polls subdirectory. But this frame is a bad idea. Django will select the first template that matches the name, and if you have a template with the same name in another app, Django will not be able to differentiate them from each other.
We need to be able to find the template that we need, the simplest way is to name the template (namespace), that is, by putting these templates in another folder named the app itself.
That's, by putting those templates inside another directory named for the application itself.
Now let's modify the code in the view belowFrom django.http import HttpResponse
From django.template import Requestcontext,loader
From Polls.models import Poll
def index (Request):
Latest_poll_list = Poll.objects.order_by ("-pub_date") [: 5]
Template = Loader.get_template ("polls/index.html")
Content = RequestContext (request,{
' Latest_poll_list ': latest_poll_list,
})
Return HttpResponse (Template.render (context))