我在Django中使用reverse
包装方法时遇到问题。
这是我的urls.py
...
urlpatterns = patterns('',
('/param_select/$',session_check(param_select)),
('registration/$',registration),
('result_show/(\d+)',session_check(result_show)),
('^expofit/$',media_clean(start)),
('result_pick/$',session_check(result_pick)),
('mail_report/$',session_check(mail_report)),
('notification/$',session_check(notification)),
我只能为reverse
成功registration
。
In [37]: from django.core.urlresolvers import reverse
In [38]: from expofit_django_app import views
In [38]: reverse(views.registration)
Out[38]: '/registration/'
In [42]: reverse(views.mail_report)
---------------------------------------------------------------------------
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(views.mail_report)
/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 'expofit_django_app.views.mail_report' with arguments '()' and keyword arguments '{}' not found.
由于所有其他包装方法的情况相同,并且它仅适用于未包装的方法,我相信它与反转包装方法有关。 解决方案的想法?
答案 0 :(得分:1)
只需为您的网址命名 - 即
('/param_select/$', session_check(param_select), {}, 'param_select'),
然后你可以使用
reverse('param_select')
另一个想法是将session_check
重写为装饰者; django内置的视图装饰器(即permission_required)将成为一个很好的起点。