Django implements sending email to reset user password

        Django is a good framework, very complete, with a built-in user system, we can send reset password emails with a little modification.

url.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from django.contrib.auth import views as auth_views

urlpatterns = patterns('',

url(r'^forgot-password/$',
views.forgot_password, name="forgot-password"),
url(r'^password/change/$',
auth_views.password_change,
name='password_change'),
url(r'^password/change/done/$',
auth_views.password_change_done,
name='password_change_done'),
url(r'^resetpassword/$',
auth_views.password_reset,
name='password_reset'),
url(r'^resetpassword/passwordsent/$',
auth_views.password_reset_done,
name='password_reset_done'),
url(r'^reset/done/$',
auth_views.password_reset_complete,
name='password_reset_complete'),

url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$',
auth_views.password_reset_confirm,
name='password_reset_confirm'),

)

templates settings

Copy the  django/contrib/auth/templates/registration following files to the registration in your templates directory:

1
password_reset_subject.txt

Copy the  django/contrib/admin/templates/registration following files to the registration in your templates directory:

1
2
3
4
5
6
7
8
logged_out .html
password_change_done .html
password_change_form .html
password_reset_complete .html
#The file for password modification password_reset_confirm .html
password_reset_done .html
password_reset_email .html #The file for sending email
password_reset_form.html

You can define it according to your own needs. I deleted the logged_out.html file, added the login.html written by myself, and then added the logged_out.html file in all files.

1
2
3
{% extends admin/base_site.html %}
改为
{% extends base.html %}

After doing this, it still doesn't work, because the base.html file is required:

1
2
3
4
5
6
7
8
<html>
<head><title>{% block title %}{% endblock title %}</title></head><body>{% block content %}{% endblock content %}</body></html>





test

Click forgot password:

Forgot passwordForgot password

Enter your own email address.

After a while you will receive an email:

Forgot passwordForgot password

The content is:

Forgot passwordForgot password

If you want to modify the content of the email, you can modify the templates/registration/password_reset_email.htmlfile.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326963243&siteId=291194637