我想在我的项目中保留一个干净的网址描述,所以我使用命名空间:
项目/ urls.py:
urlpatterns = patterns('',
url(r'^contracts/', include('apps.contracts.urls', namespace='contracts')),
url(r'^accounts/', include('apps.registration_custom.backends.vince.urls', namespace='accounts')),
registration_custom /后端/文斯/ urls.py:
urlpatterns = patterns('',
url(r'^activate/complete/$',
TemplateView.as_view(template_name='registration/activation_complete.html'),
name='registration_activation_complete'),
# Activation keys get matched by \w+ instead of the more specific
# [a-fA-F0-9]{40} because a bad activation key should still get to the view;
# that way it can return a sensible "invalid key" message instead of a
# confusing 404.
url(r'^activate/(?P<activation_key>\w+)/$',
ActivationView.as_view(),
name='registration_activate'),
url(r'^register/$',
CustomRegistrationView.as_view(),
name='registration_register'),
url(r'^register/complete/$',
TemplateView.as_view(template_name='registration/registration_complete.html'),
name='registration_complete'),
url(r'^register/closed/$',
TemplateView.as_view(template_name='registration/registration_closed.html'),
name='registration_disallowed'),
(r'', include('registration.auth_urls')),
)
使用此配置,如果我请求/ accounts / password / reset /我收到错误:
Reverse for 'django.contrib.auth.views.password_reset_done' with arguments '()' and keyword arguments '{}' not found
但是,如果我没有命名我的网址,那么我的/ accounts / password / reset / request一切都会变得干净。
我的理解是django / contrib / auth / views / password_reset视图在'django.contrib.auth.views.password_reset_done'上使用reverse()。并且网址调度员会丢失,因为它应该要求'accounts:auth_password_reset_done'。
我猜对了吗?
那么我的选择是什么?
EDIT。 重定向到/ accounts / password / reset的模板代码:
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Log in' %}" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
<p>{% trans "Forgot password" %}? <a href="{% url 'accounts:auth_password_reset' %}">{% trans "Reset it" %}</a>!</p>
<p>{% trans "Not member" %}? <a href="{% url 'accounts:registration_register' %}">{% trans "Register" %}</a>!</p>
{% endblock %}
答案 0 :(得分:1)
事实上,Django auth模块正在寻找一个视图,当他要求:reverse('django.contrib.auth.views.password_reset_done')
但我的urls配置是名称空间,所以Django丢失了(这里我不确切知道,也许有人可以更好地解释)。
所以我需要先说Django首先查看非命名空间的url模式,只是为了它的核心代码。
所以我的诀窍是在myproject / urls.py中添加一行:
urlpatterns = patterns('',
url(r'^contracts/', include('apps.contracts.urls', namespace='contracts')),
# this is tricky
# the default auth module need to reverse some urls like reverse('django.contrib.auth.views.password_reset_done')
# but I want to keep a namespaced urls config
# then I need to supply a first url path to django when he look at non-namespaced urls
url(r'^accounts/', include('registration.auth_urls')),
url(r'^accounts/', include('apps.registration_custom.backends.vince.urls', namespace='accounts')),
一切正常!
我承认这不是一个优雅的解决方案,但它有利于保持我的名称空间正确,模板也是如此。
答案 1 :(得分:0)
为include('registration.auth_urls')
添加正确的命名空间和应用名称(请参阅最后一行)
urlpatterns = patterns('',
url(r'^activate/complete/$',
TemplateView.as_view(template_name='registration/activation_complete.html'),
name='registration_activation_complete'),
# Activation keys get matched by \w+ instead of the more specific
# [a-fA-F0-9]{40} because a bad activation key should still get to the view;
# that way it can return a sensible "invalid key" message instead of a
# confusing 404.
url(r'^activate/(?P<activation_key>\w+)/$',
ActivationView.as_view(),
name='registration_activate'),
url(r'^register/$',
CustomRegistrationView.as_view(),
name='registration_register'),
url(r'^register/complete/$',
TemplateView.as_view(template_name='registration/registration_complete.html'),
name='registration_complete'),
url(r'^register/closed/$',
TemplateView.as_view(template_name='registration/registration_closed.html'),
name='registration_disallowed'),
(r'', include('registration.auth_urls'), namespace='accounts', app_name='registration'),
)