问题
I want to set up user authentication with Django (1.9). As described in the documentation I included the auth view in my project's urls.py
like
urlpatterns = [
...,
url('^accounts/', include('django.contrib.auth.urls')),
...,
]
as the documentation describes, one needs to write custom templates for the Auth views. I put those templates in the directory myproject/templates/registration/
. The problem is now that these templates, since they follow the predefined naming convention, clash with the admin templates for Auth views. E.g. if I follow in the link CHANGE PASSWORD in the admin, the admin view gets rendered with my custom template. How can I namespace my custom templates, so that they won't interfere with the admin?
回答1:
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']
回答2:
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'),
...,
]
来源:https://stackoverflow.com/questions/37439153/django-auth-where-to-put-custom-templates