Handle view and templates
View consist of a set of functions which handle the different url request with the specific url pettarns.
And it returns either of HttpResponse
or Http404
.
Firstly, let’s update webapp/views.py
:
from django.shortcuts import render
from .models import Question
def index(request):
# get the lastest 5 questions
latest_question_list = Question.objects.order_by('-pub_date')[:5]
# create context
context = {'latest_question_list': latest_question_list}
# a shortcuts for render request by template
return render(request, 'webapp/index.html', context)
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'webapp/detail.html', {'question': question})
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
Here we used template webapp/index.html
, which locates in webapp/tempaltes/webapp/index.html
. So let’s create a folder templates
and its subfolder webapp
, the code of index.html
:
# list of all items from question object
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
# webapp is the namespace, detail is the name
<li><a href="{% url 'webapp:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}</ul>
{% else %}
No polls are available.
{% endif %}
The trick point is when we refer to the details, we use {% url 'webapp:detail' question.id %}
instand of absolute path. Here webapp
is the name space, detail is the name, all can be found in updated webapp/urls.py
:
from django.urls import path
from . import views
# namespace
app_name = 'webapp'
urlpatterns = [
# ex: /webapp/
path('', views.index, name='index'),
# ex: /webapp/5/
path('<int:question_id>/', views.detail, name='detail'),
# ex: /webapp/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /webapp/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
```</int:question_id></int:question_id></int:question_id>
Similar to the template `index.html`, we should add the template `webapp/tempaltes/webapp/detail.html`:
``` html
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}</ul>
All the dynamic codes in html are easy to understand, I wouldn’t waste time to explain them.
Now, you can access http://localhost:8000/webapp/
to display the reuslts.The whole process can be described like this:
1. send request to server
2. Djongo pastes the url by ROOT_URLCONF = 'mysite.urls'
in mysite/settings.py
, which points to mysite.urls
.
3. In term of urlpatterns
in mysite/urls.py
, the request will be transfered to webapp
folder.
4. The request can be handled by webapp/urls.py
, which points to the different functions in webapp/views.py
. Here the second param in the function results
is from request pattern.
5. View pastes and handle the request, then retrieves template in template/webapp/
.
6. HttpResponse or Http404 back to client
In a nutshell, urls.py
handles url patterns and sends request to views.py
, views.py
calls model.py
and templates to send response back.