由于更改 URL 中的顺序(身份验证/注册)而导致的不同模板使用

Different templates usage caused by changing the order in URLs (auth / registration)

我在我的项目中使用 built-in auth tools 和 django-registration

我的注销模板位于:

/accounts/templates/registration/logout.html

如果 urls.py 看起来像:

urlpatterns = [
...
url(regex = r'^accounts/', view = include('registration.backends.hmac.urls')),
url(regex = r'^accounts/', view = include('django.contrib.auth.urls')),
...
]

它使用我的模板。没关系。

但是如果我重新组织 url 就像:

 urlpatterns = [
...
url(regex = r'^accounts/', view = include('django.contrib.auth.urls')),
url(regex = r'^accounts/', view = include('registration.backends.hmac.urls')),
...
]

它使用内置的管理员注销模板。

为什么会这样?

编辑

their tutorial 我看到他们说 'registration.backends.hmac.urls':

That URLconf also sets up the views from django.contrib.auth (login, logout, password reset, etc.), though if you want those views at a different location, you can include() the URLconf registration.auth_urls to place only the django.contrib.auth views at a specific location in your URL hierarchy.

但是我打开的时候好像和auth没有关系urls/views: 编辑:好的,现在我明白了。

"""
URLconf for registration and activation, using django-registration's
HMAC activation workflow.

"""

from django.conf.urls import include, url
from django.views.generic.base import TemplateView
from .views import ActivationView, RegistrationView


urlpatterns = [
url(r'^activate/complete/$',
    TemplateView.as_view(
        template_name='registration/activation_complete.html'
    ),
    name='registration_activation_complete'),
# The activation key can make use of any character from the
# URL-safe base64 alphabet, plus the colon as a separator.
url(r'^activate/(?P<activation_key>[-:\w]+)/$',
    ActivationView.as_view(),
    name='registration_activate'),
url(r'^register/$',
    RegistrationView.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'),
url(r'', include('registration.auth_urls')),

]

last url pattern in registration.backends.hmac.urls includes registration.auth_urls,提供登录、注销等url。

url(r'', include('registration.auth_urls')),

如果您在 hmac url 上方包含 django.contrib.auth.urls,则将使用来自 django.contrib.auth 的注销视图。此视图 uses a different templateregistration/logged_out.html。由于您没有覆盖它,因此使用管理模板。

将 urls 映射到视图的 django url dipatcher 按从头到尾的顺序检查 urls:

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.

所以相似url的顺序决定了哪个url被匹配。 IE。第一个匹配。