Django 1.7.4:Django 1.7 tutorial04上的错误04:“投票"的Reverse的NoReverseMatch错误 [英] Django 1.7.4: Error on Django 1.7 tutorial04: NoReverseMatch error for Reverse for 'vote'
问题描述
我整个上午一直在努力地完成Django 1.7 tutorial04的工作.我已经阅读了关于OverReflowMatch错误的所有关于stackoverflow的类似文章,我的错误略有不同:
I have been banging my head against the wall all morning trying to complete the Django 1.7 tutorial04. I have read through all the similar posts on stackoverflow regarding the NoReverseMatch error, my error is slightly different:
未找到带有参数'('',)'和关键字参数'{}'的'vote'反向字符.尝试了1种模式:[u'polls/(?P< question_id> \\ d +)/vote/$']
.
看来我的details.html表单动作属性令人king目结舌:
It appears it is choking on my details.html form action attribute:
<body bgcolor="white">
<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>
</body>
我已经在主URL中定义了民意调查名称空间,并使其在我的民意调查索引页面上工作-ul li中的标记正在使用 {%url'polls:detail'question.id%}
,效果很好.
I have already defined the polls namespace in my main urls and got it working on my polls index page - the a tag inside the ul li is using {% url 'polls:detail' question.id %}
and it works just fine.
我的民意调查/urls.py看起来像:
My polls/urls.py looks like:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$',views.index, name='index'),
url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
)
最后,我的民意调查/views.py:
Finally my polls/views.py:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse,Http404
from django.http import HttpResponseRedirect, HttpResponse
from polls.models import Question,Choice
# Create your views here.
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', {'question':question})
def results(request, question_id):
response = HttpResponse("You're looking at the results of question %s.")
return HttpResponse(response % question_id)
def vote(request, question_id):
p = get_object_or_404(Question, pk=question_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# redisplay the question voting form
return render(request, 'polls/detail.html', {
'question': p,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
select_choice.save()
# always return response redirect after dealing with POST
# to prevent reposts if user hits back button
return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))
错误:
NoReverseMatch at /polls/1/
Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<question_id>\\d+)/vote/$']
Request Method: GET
Request URL: http://localhost:8000/polls/1/
Django Version: 1.7.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<question_id>\\d+)/vote/$']
Exception Location: /usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 468
Python Executable: /usr/bin/python
Python Version: 2.7.9
任何帮助将不胜感激.谢谢您的阅读.
Any assistance would be greatly appreciated. Thank you for reading.
推荐答案
而不是 question_id
,请尝试在模板中使用 question.id
,即:
Instead of question_id
, try using question.id
in the template, i.e.:
<form action="{% url 'polls:vote' question.id %}" method="post">
这篇关于Django 1.7.4:Django 1.7 tutorial04上的错误04:“投票"的Reverse的NoReverseMatch错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!