Sitemap
Press enter or click to view image in full size
article banner

Django Polls Simplified — Writing more views

3 min readJan 19, 2025

--

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.")


def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)


def results(request, question_id):
response = "You're looking…

--

--

Chris Achinga
Chris Achinga

Written by Chris Achinga

Software Engineer (Python & Javascript)

No responses yet