我在Django应用中使用自定义模板标记。在尝试拨打reverse
以获取网址时,我收到NoReverseMatch
错误,尽管该应用中的其他地方也有同样的呼叫。
在portal/templatetags/custom_tags.py
:
@register.simple_tag()
def breadcrumb(view, text, *args):
return u'» <a href="' + reverse(view, args) + '">' + text + '</a>'
在模板中:
{% breadcrumb 'portal-home' 'Portal' %}
在portal/urls.py
:
urlpatterns = [
# home
url(r'home/$', portal_home, name='portal-home'),
...
]
肯定有一个名为portal-home
的视图,当我在应用程序的其他位置尝试reverse('portal-home')
时(例如在视图中),它可以正常工作。
这是确切的错误:
Reverse for 'portal-home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
答案 0 :(得分:0)
原来,我忘了如何蟒蛇。我需要这样做:
reverse(view, *args)