创建第一个Django app-part4
写一个最小的表单
编辑 detail.html
点击查看代码
<form action="{% url 'polls:vote' question.id%}" method="post">
{% csrf_token %}
<fieldset>
<legend><h1>{{ question.question_text }}</h1></legend>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
{% 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 %}
</fieldset>
{% if not error_message %}
<input type="submit" value="Vote">
{% endif %}
</form>
注意这里使用了命名空间polls:,所以需要先添加,修改polls/urls.py, 添加如下代码,同时之前的地址前面都需要加上polls:
app_name = "polls"
修改polls/views.py中的vote,detail,results方法
点击查看代码
from django.http.response import Http404
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponse, HttpResponseRedirect
from .models import Question, Choice
from django.template import loader
from django.urls import reverse
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
choice_counts = question.choice_set.count()
if choice_counts<=0:
return render(request, 'polls/detail.html', {"question": question, "error_message": "You didn't select a choice."})
else:
return render(request, 'polls/detail.html', {"question": question})
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, "polls/results.html", {"question": 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):
return render(request, 'polls/detail.html', {"question": question, "error_message": "You didn't select a choice."})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse("polls:results", args=(question_id, )))
创建模板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>
修改view和url.py
修改polls/urls.py
点击查看代码
from os import name
from django.urls import path
from . import views
app_name = "polls"
urlpatterns = [
# path("", views.index, name="index"),
# path("<int:question_id>/", views.detail, name="detail"),
# path("<int:question_id>/results/", views.results, name="results"),
# path("<int:question_id>/vote/", views.vote, name="vote"),
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"),
]
修改polls/views.py
点击查看代码
from django.views import generic
class IndexView(generic.ListView):
template_name = "polls/index.html"
context_object_name = "latest_question_list"
def get_queryset(self) -> QuerySet[Any]:
return Question.objects.order_by("-pub_date")[:5]
'''
这种写法,我尝试过加上其它的上下文,例如detail.html中存在的error_message,
但是报错了,应该是model这个属性和其它上下文不能同时存在。
所以还是用方法比较好,更加的简单,容易理解也更灵活
'''
class DetailView(generic.DetailView):
model = Question
template_name = "polls/detail.html"
class ResultsView(generic.DetailView):
model = Question
template_name = "polls/results.html"

浙公网安备 33010602011771号