[Django]-Django auth: Where to put custom templates?

3👍

You should override template names for built-in functions:

urlpatterns = [
    url(
        '^change-password/',
        auth_views.password_change,
        {'template_name': 'myproject/registration/change-password.html'}
    )
]

As the doc describes, you have to use the next templates in urls.py:

^login/$ [name='login']
^logout/$ [name='logout']
^password_change/$ [name='password_change']
^password_change/done/$ [name='password_change_done']
^password_reset/$ [name='password_reset']
^password_reset/done/$ [name='password_reset_done']
^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$ [name='password_reset_confirm']
^reset/done/$ [name='password_reset_complete']

3👍

As Aleksander Gordienko pointed out, including each auth view manually and changing the template name seems to be the best solution. To save you some typing and manual extraction of the auth view names, here are the url patterns, where the templates are stored under their original name but in the directory project/myapp/templates/myapp/registration/.

from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r'^accounts/login/$', auth_views.login, 
        {'template_name': 'myapp/registration/login.html'}, name='login'),
    url(r'^accounts/logout/$', auth_views.logout, 
        {'template_name': 'myapp/registration/logout.html'}, name='logout'),
    url(r'^accounts/password_change/$', auth_views.password_change,
            {'template_name': 'myapp/registration/password_change_form.html',
             'post_change_redirect': '/accounts/password_change/done/'}, 
            name='password_change'),
    url(r'^accounts/password_change/done/$', auth_views.password_change_done,
            {'template_name': 'myapp/registration/password_change_done.html'}, 
            name='password_change_done'),
    url(r'^accounts/password_reset/$', auth_views.password_reset,
            {'template_name': 'myapp/registration/password_reset_form.html'}, 
            name='password_reset'),
    url(r'^accounts/password_reset/done/$', auth_views.password_reset_done,
            {'template_name': 'myapp/registration/password_reset_done.html'}, 
            name='password_reset_done'),
    url(r'^accounts/reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 
        auth_views.password_reset_confirm,
            {'template_name': 'myapp/registration/password_reset_confirm.html'}, 
            name='password_reset_confirm'),
    url(r'^accounts/reset/done/$', auth_views.password_reset_complete,
            {'template_name': 'myapp/registration/password_reset_complete.html'}, 
            name='password_reset_complete'),
    ...,
]
👤Jarno

Leave a comment