【问题标题】:NoReverseMatch using URLConf & Generic Views in Django 1.4.xNoReverseMatch 在 Django 1.4.x 中使用 URLConf 和通用视图
【发布时间】:2013-02-01 01:41:52
【问题描述】:

我的模板文件中有这个:

<a href="{% url polls.views.vote poll.id %}">Vote again?</a>

这是我的 URLConf:

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

我更改了一些视图以使用泛型:

urlpatterns = patterns('polls.views',
    url(r'^$',
        ListView.as_view(
            queryset=Poll.objects.order_by('-pub_date')[:5],
            context_object_name='latest_poll_list',
            template_name='polls/index.html')),
    url(r'^(?P<pk>\d+)/$',
        DetailView.as_view(
            model=Poll,
            template_name='polls/details.html')),
    url(r'^(?P<pk>\d+)/results/$',
        DetailView.as_view(
            model=Poll,
            template_name='polls/results.html'),
        name='results'),
    url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)

现在显示此错误:

未找到带有参数“(1,)”和关键字参数“{}”的“polls.views.results”的反向操作。

我该如何解决这个问题?

【问题讨论】:

    标签: django views urlconf


    【解决方案1】:

    将名称添加到您的 url 模式:

    url(r'^(?P<pk>\d+)/results/$',
        DetailView.as_view(
            model=Poll,
            template_name='polls/results.html'),
        name='results'),
    

    然后在模板中使用名称而不是视图名称:

    {% url results poll.id %}
    

    【讨论】:

    猜你喜欢
    • 2012-10-13
    • 2015-10-20
    • 2012-06-09
    • 2011-04-23
    • 2012-06-10
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    相关资源
    最近更新 更多