Does Django ship with the authentication templates

2019-01-26 10:55发布

This question already has an answer here:

I found some under the tests directory but I'm not sure if they are the right ones.

By authentication templates I mean login.htm, password_reset.htm, etc.

Some of the templates can be found at: http://devdoodles.wordpress.com/2009/02/16/user-authentication-with-django-registration/

4条回答
放荡不羁爱自由
2楼-- · 2019-01-26 11:36

You can use the auth templates at django.contrib.admin.templates.registration:

logged_out.html
password_change_done.html
password_change_form.html
password_reset_complete.html
password_reset_confirm.html
password_reset_done.html
password_reset_email.html
password_reset_form.html

Those will have the look and feel of the Django Admin, so I would suggest to customize it.

查看更多
Melony?
3楼-- · 2019-01-26 11:44

No, it looks for those templates in a "registration" directory within your templates folder.

From the docs:

It's your responsibility to provide the login form in a template called registration/login.html by default.

Password Reset Optional arguments:

template_name: The full name of a template to use for displaying the password reset form. This will default to registration/password_reset_form.html if not supplied.

Docs: login, password_reset

查看更多
混吃等死
4楼-- · 2019-01-26 11:44

By copying the templates located in django.contrib.admin.templates.registration as mentioned by DZPM above and placing them into your own registration app's templates directory e.g. *your_proj_root/registration/templates/registration/*

IMPORTANT! If you are keeping the same exact filenames for your templates, you have to remember to make sure that your django.contrib.admin app line is placed below your registration app line; otherwise, it will use the django.contrib.admin's registration templates in preference.

查看更多
做个烂人
5楼-- · 2019-01-26 11:55

While the Django documentation explicitly states that "Django provides no default template for the authentication views", I found that it is trivial to use the admin templates. Just enable the admin app, then add this to urls.py:

url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}),
url('^accounts/', include('django.contrib.auth.urls')),

All of the authentication urls work now, albeit with the Django admin look-and-feel.

查看更多
登录 后发表回答