gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:59:53 25 4
gpt4 key购买 nike

这里指的是 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

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

我的详细信息.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>

我的民意调查/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,)))

最佳答案

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

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

关于python - Django 教程第 4 部分中的 NoReverseMatch 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27882335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com