自定义模板标记中的reverse()使用NoReverseMatch

时间:2016-03-22 01:18:26

标签: django django-templates django-views

我在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: []

1 个答案:

答案 0 :(得分:0)

原来,我忘了如何蟒蛇。我需要这样做:

reverse(view, *args)