This refers to the django 1.6 tutorial.
这是指django 1.6教程。
I keep getting this error message when I try to open the /polls/1/ page in part 4 of the tutorial:
当我尝试打开教程第4部分中的/ polls / 1 /页面时,我不断收到此错误消息:
NoReverseMatch at /polls/1/
Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<poll_id>\\d+)/vote/$']
Request Method: GET
Request URL: http://*/polls/1/
Django Version: 1.6.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P<poll_id>\\d+)/vote/$']
Exception Location: /home/iamal/lib/python2.7/Django-1.6.4-py2.7.egg/django/core/urlresolvers.py in _reverse_with_prefix, line 452
Python Executable: /package/host/localhost/python-2.7.3/bin/python
Python Version: 2.7.3
Python Path:
['/home/iamal/tutorial',
'/home/iamal/bin',
'/home/iamal/lib/python2.7/Django-1.6.4-py2.7.egg',
'/home/iamal/lib/python2.7/gunicorn-19.1.1-py2.7.egg',
'/package/host/localhost/python-2.7.3/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg',
'/package/host/localhost/python-2.7.3/lib/python2.7/site-packages/distribute-0.6.35-py2.7.egg',
'/package/host/localhost/python-2.7.3/lib/python27.zip',
'/package/host/localhost/python-2.7.3/lib/python2.7',
'/package/host/localhost/python-2.7.3/lib/python2.7/plat-linux2',
'/package/host/localhost/python-2.7.3/lib/python2.7/lib-tk',
'/package/host/localhost/python-2.7.3/lib/python2.7/lib-old',
'/package/host/localhost/python-2.7.3/lib/python2.7/lib-dynload',
'/package/host/localhost/python-2.7.3/lib/python2.7/site-packages',
'/home/iamal/lib/python2.7']
Server time: So, 11 Jan 2015 00:03:43 +0100
I noticed that there seems to be no argument to be passed in the error message. When I replaced poll.id
from line 5 in the details.html with '1'
, it works. I have also checked that I included namespace="polls"
in the project/urls.py file which seems to have been a common issue.
我注意到在错误消息中似乎没有传递参数。当我将details.html中的第5行的poll.id替换为'1'时,它可以正常工作。我还检查过我在project / urls.py文件中包含了namespace =“polls”,这似乎是一个常见的问题。
My details.html:
我的details.html:
<h1>{{ poll.question }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' poll.id %}" method="post">
{% csrf_token %}
{% for choice in poll.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>
My polls/views.py:
我的民意调查/ views.py:
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.template import Context, loader
from django.core.urlresolvers import reverse
from polls.models import Poll, Choice
def index(request):
latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
context = {'latest_poll_list': latest_poll_list}
return render(request, 'polls/index.html', context)
def detail(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id)
return render(request, 'polls/detail.html', {'poll': poll_id})
def results(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id)
return render(request, 'polls/results.html', {'poll': poll})
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render(request, 'polls/detail.html', {
'poll': p,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))
1 个解决方案
#1
1
Your detail view is passing the poll_id to the template target instead of the poll object itself. It should be:
您的详细信息视图将poll_id传递给模板目标而不是poll对象本身。它应该是:
return render(request, 'polls/detail.html', {'poll': poll})
#1
1
Your detail view is passing the poll_id to the template target instead of the poll object itself. It should be:
您的详细信息视图将poll_id传递给模板目标而不是poll对象本身。它应该是:
return render(request, 'polls/detail.html', {'poll': poll})