1.表单form的使用
1.更新detail增加表单
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
解释:
每个单选框的名字是choice,意味着某人选中一个单选框并点击提交时,会发送post数据choice=#,#是所选的id。
提交会修改服务端数据时,就应该使用POST方法
forloop.counter指示进行了多少次循环。
{% csrf_token %}是为了解决跨站请求的风险。
- 修改vote方法处理请求
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from .models import Choice, Question
# ...
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
解释:
request.POST是一个类字典对象,让我们能够通过提交的关键字访问数据
request.POST[‘choice’]会引发KeyError异常,如果choice未出现在POST数据中。
HttpResponseRedirect接受一个参数URL。成功处理POST数据后,应该总是返回HttpResponseRedirect。这回阻止当用户点击回退按钮时数据被提交两次的异常。
reverse构造URL,避免使用硬编码的URL。我们传递了一个view name参数和URL可变部分的变量
3.接着构造result视图
from django.shortcuts import get_object_or_404, render
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/results.html', {'question': question})
5.创建polls/results.html模板
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
这里有个问题,如果两个用户几乎同时对一个选择进行投票,那么只会计算一次,为了避免这种情况可以参考:
https://docs.djangoproject.com/en/2.0/ref/models/expressions/#avoiding-race-conditions-using-f
通用视图generic view
有一类很常见的情形:根据URL中传递的参数从数据库中获取数据,加载模板并返回渲染后的模板。为此django提供了一个快捷方式generic views。通用视图将常见模式抽象到你不需要通过编写python代码来编写app。
为此需要如下几步:
1.修改URLconf
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
2.修改视图
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views import generic
from .models import Choice, Question
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
def vote(request, question_id):
... # same as above, no changes needed.
解释
这里我们使用了两个generic view:ListView , DetailView。这两个view分别抽象了“显示对象列表”和”显示特别类型对象的详细信息”。
每个generic view需要知道他将执行什么模型,通过model属性提供。
DetailView将从URL中捕获到”pk”并作为主键。因此我们将URLconf中的question_id改为pk。
DetailView默认使用的模板称为< app name>/< model name>_detail.html。通过template_name指定不同的模板。为results提供template_name是为了确保渲染后他们的呈现形式不一样。
同理ListView使用的默认模板为< app name>/< model name>_list.html
在之前的教程中,我们为模板提供了包含question , latest_question_list的上下文变量。对于DetailView,question变量会自动生成,由于我们使用了django模型,它能根据模型的属性名自动提供上下文变量。对于ListView自动生成的变量是question_list , 为了覆盖这个,我们提供了context_object_name,指定我们需要lastest_question_list