上一节,我们简单介绍了一下index视图,其他三个视图没有补全,现进行说明:
detail.html视图适用于投票页面
<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>
说明:error_message是当view中捕获到异常后,填写error_message信息,然后回传回页面
form表单中有 单选按钮,label,按钮,采用post方式发送,并且url发送添加token,防止url被篡改
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(