Django教程第4部分中的NoReverseMatch错误

时间:2015-01-10 23:36:49

标签: python django

这是指django 1.6教程。

当我尝试打开教程第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

我注意到在错误消息中似乎没有传递参数。当我使用poll.id替换details.html中第5行的'1'时,它就可以了。我还检查过我在项目/ urls.py文件中包含namespace="polls",这似乎是一个常见的问题。

我的详情:

<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>

我的民意调查/ 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 个答案:

答案 0 :(得分:1)

您的详细信息视图是将poll_id传递给模板目标而不是poll对象本身。它应该是:

return render(request, 'polls/detail.html', {'poll': poll})