In Django, views are Python functions or classes that handle web requests, process data, and return responses, typically by rendering templates or returning JSON.
In our poll application, we’ll have the following four views:
Question “index” page — displays the latest few questions.
The question “detail” page displays question text with no results but a form to vote.
Question “results” page — displays results for a particular question.
Vote action — handles voting for a specific choice in a particular question.
In Django, web pages and other content are delivered by views. Each view is represented by a Python function (or method, in the case of class-based views). Django will choose a view by examining the requested URL (to be precise, the part of the URL after the domain name).
Now, let’s add a few more views to polls/views.py. These views are slightly different because they take an argument:
from django.http import HttpResponse
def index(request): return HttpResponse("Hello, world. You're at the polls index.")