我正在关注Django Tutorial的官方网页。我是本教程的第4部分。在编写了results.html,detail.html并根据它的文档更新了视图后,我在尝试从浏览器访问/ polls / 1时收到了下面提到的错误。我无法找到错误的位置,因为一切似乎都是根据教程。 / polls /和民意调查/ 1 /投票
没有错误/ polls / 1 /
的NoReverseMatch反向投票'有参数'('',)'和关键字参数' {}'未找到。尝试了1种模式:['民意调查/(?P [0-9] +)/投票/ $']
我的民意调查/ urls.py文件
from django.conf.urls import url
from . import views
app_name = 'polls'
url patterns = [
url(r'^$',views.index,name='index'),
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
我的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>
我的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>
和我的民意调查/ views.py文件是
from django.shortcuts import get_object_or_404, render
from django.http import Http404
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.template import loader
from .models import Choice, Question
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {
'latest_question_list': latest_question_list,
}
return render(request, 'polls/index.html', context)
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'queston':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):
#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()
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
我的root / urls.py文件是
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/',include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
答案 0 :(得分:1)
在detail
视图中呈现的上下文中存在拼写错误。它是question
而不是queston
。
换句话说,您将question
对象作为queston
传递给detail.html
模板。
return render(request, 'polls/detail.html', {'queston': question})
应该是:
return render(request, 'polls/detail.html', {'question': question})