Search code examples
djangodjango-templatesdjango-urlsdjango-authenticationdjango-registration

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


I am using in my project built-in auth tools and django-registration

I have my logout template at:

/accounts/templates/registration/logout.html

If urls.py looks like:

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

It uses my template. It's OK.

But if I reorganize url like:

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

It uses built-in admin logout template.

Why does it happen?

Edit

In their tutorial I see that they say about '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.

But when I open it, it seems to have no connection with auth urls/views: EDIT: OK, now I see.

"""
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')),

]


Solution

  • The last url pattern in registration.backends.hmac.urls includes registration.auth_urls, which provides urls for login, logout and so on.

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

    If you include django.contrib.auth.urls, above the hmac urls, then the logout view from django.contrib.auth will be used. This view uses a different template, registration/logged_out.html. Since you haven't overridden this, the admin template is used.