url标签拒绝查找视图,即使它位于views.py和urls.py中

时间:2012-02-14 11:48:09

标签: django django-templates

标题几乎总结了一下 - 我在模板中有这个标签:

<a href="{% url mainapp.views.send_registration_email_view user_email=for_user.email,company_number=company.pk,organisation='org' %}">

在urls.py:

(r'^send/registrationlink/(?P<user_email>[^/]+)/(?P<company_number>[^/]+)/(?P<organisation>\w+)/?$', 'mainapp.views.send_registration_email_view'),

,这在mainapp / views.py中:

def send_registration_email_view(request, user_email = None, company_number = None, organisation = None):

但是,尝试查看导入找到有问题行的模板的页面会导致此错误:

TemplateSyntaxError: Caught NoReverseMatch while rendering: Reverse for 'mainapp.views.send_registration_email_view' with arguments '()' and keyword arguments '{'organisation': u'org', 'company_number': u'UN58e2391e-51a5-11e1-bb6a-e89a8f7f9c14', 'user_email': ''}' not found.

我在同一个模板中也有这些标签,它们工作正常:

<a href="{% url mainapp.views.registration_view for_user=for_user %}">
<a href="{% url mainapp.views.consent_view for_user=for_user %}">

所以 - 有谁知道为什么django不喜欢我正在做的事情?

1 个答案:

答案 0 :(得分:3)

正则表达式的这一部分:(?P<user_email>[^/]+)期望至少有一个字符,但是您将空字符串作为user_email传递。在正则表达式中,+表示“一个或多个”。将其更改为*也接受空字符串。

http://docs.python.org/library/re.html#regular-expression-syntax