使用参数反向命名包装方法的URL

时间:2013-03-12 12:08:27

标签: django django-templates django-views

我正在尝试对命名包装方法的URL进行反向解析。 我在urls.py中设置了以下内容(基于this answer

urlpatterns = patterns('',
('param_select/$',session_check(param_select),{}, 'param_select'),
('registration/$',registration),
('result_show/(\d+)',session_check(result_show),{}, 'result_show'),
('^expofit/$',media_clean(start),{}, 'start'),
('result_pick/$',session_check(result_pick),{}, 'result_pick'),
('mail_report/$',session_check(mail_report)),
('notification/$',session_check(notification)),

它适用于没有参数的所有方法。然而,在接受参数的那个上失败了。

In [50]: from django.core.urlresolvers import reverse

In [51]: reverse('result_pick')
Out[51]: '/result_pick/'

In [52]: reverse('start')
Out[52]: '/expofit/'

In [53]: reverse('result_show', args=(2,))
---------------------------------------------------------------------------
NoReverseMatch                            Traceback (most recent call last)
/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 reverse('result_show', args=(2,))

/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/core/urlresolvers.pyc in reverse(viewname, urlconf, args, kwargs, prefix, current_app)
    474             resolver = get_ns_resolver(ns_pattern, resolver)
    475 
--> 476     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
    477 
    478 reverse_lazy = lazy(reverse, str)

/home/alan/Desktop/expofit/expofit_env/local/lib/python2.7/site-packages/django/core/urlresolvers.pyc in _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs)
    394             lookup_view_s = lookup_view
    395         raise NoReverseMatch("Reverse for '%s' with arguments '%s' and keyword "
--> 396                 "arguments '%s' not found." % (lookup_view_s, args, kwargs))
    397 
    398 class LocaleRegexURLResolver(RegexURLResolver):

NoReverseMatch: Reverse for 'result_show' with arguments '(2,)' and keyword arguments '{}' not found.

我一直在关注这本书,除了名字url everythong看起来一样。关于缺陷在哪里的想法?

1 个答案:

答案 0 :(得分:0)

问题在于我使用的是Django 1.4版本,并阅读了Django 1.5文档。更新后,一切正常。

In [5]: reverse('result_show',args=(3,))
Out[5]: '/result_show/3'