python - 我需要在 djangoproject 教程 4 中渲染(请求...)或 HttpResponseRedirect(反向...)

标签 python django

在 djangoproject 教程第 4 部分中,我收到以下错误:

NoReverseMatch at /polls/1/results/ Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P\d+)/$']

1   <h1>{{ question.question_text }}</h1>
2   
3   <ul>
4   {% for choice in question.choice_set.all %}
5       <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
6   {% endfor %}
7   </ul>
8   
9   <a href="{% url 'polls:detail' question.id %}">Vote again?</a>
10  

看来我写的和教程中的内容一模一样,有谁知道可能出了什么问题吗?

我尝试了 stackedoverflow 中的其他方法,例如: Django 1.7.4: Error on Django 1.7 tutorial04: NoReverseMatch error for Reverse for 'vote'

http://djangotalk.blogspot.com/2014/12/re-reverse-for-results-with-arguments_2.html

但是所有这些错误都可能指向不同的事情

我的代码如下:

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:

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext, loader
from polls.models import Question, Choice
from django.http import Http404
from django.core.urlresolvers import reverse


def index(request):
    latest_question_list =  Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = RequestContext(request, {'latest_question_list' : latest_question_list, })
    return HttpResponse(template.render(context))

def detail(request, question_id):
    try:
        question = Question.objects.get(pk = question_id)
    except Question.DoesNotExist:
        raise Http404("Question ne postoji")
    return render(request, 'polls/detail.html', {'question': question})

def results(request, question_id):
    question = get_object_or_404(Question, pk = question_id)
    return render(request, 'polls/results.html', {'guestion': question})


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):
        return render(request, 'polls/detail.html', {
            'question': p,
            'error_message': "Did not choose answer.",
         })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))

最佳答案

您的结果 View 有拼写错误:

return render(request, 'polls/results.html', {'guestion': question})
                                               ^ g instead of q

因此,您的模板没有 question 值,因此反向 URL 解析失败 - 您可以看到错误消息表明它正在为 id 传递空白值>,而不是 question.id 的值。

关于python - 我需要在 djangoproject 教程 4 中渲染(请求...)或 HttpResponseRedirect(反向...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30094828/

相关文章:

python - uWSGI AJAX,读取请求

python - 查找连续七个 "7"的所有 10 位素数 - Python

python - 如何让 strftime 输出年度季度?

django - mod_wsgi 无法连接 WSGI 守护进程

python - Graphviz - 属性错误 : object has no attribute 'partition'

css - css 中的 django 变量 - 我可以使用模板吗

python - django : ImportError No module named myapp. views.hometest

python - unique_together 中的多个元组

mysql - 如何使用 pymysql (python 3.5) 使用 mysql 配置 django

python - 我怎样才能简单地计算 python 中时间序列的滚动/移动方差?