Django:为' detail'反向带有参数'('',)'和关键字参数' {}'未找到 [英] Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found

查看:124
本文介绍了Django:为' detail'反向带有参数'('',)'和关键字参数' {}'未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照官方教程学习Django并使用1.5.

I'm following the official tutorial to learn Django and using 1.5.

我将此链接作为索引模板的一部分,工作正常:

I had this link as part of my index template, which was working fine:

<li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>

但是,这是硬编码的,本教程建议使用以下更好的方法:

however, this is hardcoded and the tutorial suggested a better way was to use:

<li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li>

这样您在处理大量模板时会更好,并且您必须对URL进行更改.

so that you'll be better of when dealing with huge number of templates and u have to make changes to the url.

由于进行了上述更改,因此在运行应用程序时出现以下错误:

Since I made the above change I get the following errors when I run the app:

Exception Type: NoReverseMatch
Exception Value:    Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found.

我的urls.py看起来像这样:

My urls.py looks like this:

from django.conf.urls import patterns, url

from polls import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
    url(r'^(?P<poll_id>\d+)/results/$', views.results, name='results'),
   url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),                     
)

views.py看起来像这样:

views.py looks like this:

from django.shortcuts import render, get_object_or_404
from django.http import Http404

from polls.models import Poll

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

我的index.html模板如下所示:

my index.html template looks like this:

{% if latest_poll_list %}
    <ul>
    {% for poll in latest_poll_list %}
        <li><a href="{% url 'polls:detail' poll_id %}">{{ poll.question }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p> No polls are available.</p>
{% endif %}

通常,我可以轻松地读取错误的出处并进行处理,但是在这种情况下,我无法找出错误的原因,因此无法继续学习.任何帮助将不胜感激.

Usually I could easily read where the error is coming from and deal with it but in this case I can't spot the cause of the error hence I'm unable to progress with my study. Any help will be greatly appreciated.

推荐答案

在您的 index.html 中,您给出了 poll_id 作为自变量,但这仅是自变量的名称将包含在 detail 函数中;它没有在模板中定义.您要用来调用该函数的实际值可能是 poll.id .

In your index.html you gave poll_id as an argument, but that's just the name the argument will have within the detail function; it is not defined in your template. The actual value you want to call the function with is probably poll.id.

这篇关于Django:为&amp;#39; detail&amp;#39;反向带有参数&amp;#39;(&amp;#39;&amp;#39;,)&amp;#39;和关键字参数&amp;#39; {}&#39;未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆