1

我要保持一個乾淨的URL描述我謨,所以我使用的命名空間:命名空間的URL在Django登記

項目/ 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 /後端/文斯/網址的.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')), 
        ) 

有了這個配置,如果我請求/帳號/密碼/ 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'而迷路。

我猜對了嗎?

那麼我的選擇是什麼?

編輯。 重定向到的模板代碼/帳戶/密碼/復位:

{% 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

你可以顯示你的模板代碼重定向到'/ account/password/reset /'嗎? – sachitad

+0

@sachitad是的,我編輯我的文章 – user777466

回答

1

娃哈哈,我做了在步驟我的Django的理解!

事實上,Django的驗證模塊正在尋找一個觀點,當他詢問:reverse('django.contrib.auth.views.password_reset_done')

但我的網址配置是命名空間,所以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')), 

而且一切正常!

我承認這不是一個優雅的解決方案,但它有利於保持我的名稱空間正確,所以將模板。

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'), 
        ) 
+0

不工作,完全相同的錯誤信息 – user777466