As technology develops, there are more ways to verify a user’s identity — fingerprints, faces, voice prints — but passwords are still the most important.
There are passwords everywhere on the Internet, and even become a burden for modern people. For someone like me, who has dozens of passwords, it’s easy to forget a few of them.
This chapter explains how to help amnesiac patients and reset user passwords.
Installing third-party Libraries
We’ve already seen how to change the title, body, and so on, but passwords, as important passwords for authentication, must be changed in a more secure manner. A common way to do this is to send a password change email to the user’s email address.
Business process analysis is as follows:
- Sends an email containing the password reset address to the user’s mailbox. Email addresses need to be dynamically generated to prevent malevolent users from making trouble;
- Show the site user a successful email message;
- The user clicks the address in the mailbox and goes to the page for password reset.
- The user is presented with a message indicating that the reset succeeded.
The above four steps consist of four views and templates, and writing your own code may seem tedious.
You might be wondering if Django, a framework known for its efficiency, has a built-in function to reset passwords. The answer is yes. In fact, the flow of the built-in module is exactly the same as above, you only need to configure the URL of the above 4 steps to use it. Of course the built-in template is very simple, you can overwrite the template to make your website style.
In fact, Django doesn’t just come with password reset, it also includes login, logout, and password change functions. It is recommended that you read Django’s source code at a certain level to learn some programming skills. Also, this section of Django is written in the class view, so it might be a little hard to read right now.
Source location: / env/Lib/site – packages/django/contrib/auth/views. Py
Official documentation: Django’s validation system
Using built-in modules seems much simpler, but could it be any simpler? It can.
Django is a great Web framework, and there are a number of great third-party libraries (i.e., apps) that programmers around the world have packaged and posted online for free. Success always stands on the shoulders of giants. Why build another wheel when you already have one?
We can use a third-party library called Django-password-reset here.
To open the virtual environment, enter PIP install -u django-password-reset:
(env) E:\django_project\my_blog>pip install -U django-password-reset
Collecting django-password-reset
...
Installing collected packages: django-password-reset
Successfully installed django-password-reset-2.0
Copy the code
If the preceding information is displayed, the installation is successful.
Quick to use
/my_blog/settings.py = /my_blog/settings.py = /my_blog/settings.py = /my_blog/settings.py = /my_blog/settings.py
/my_blog/settings.py
...
INSTALLED_APPS = [
...
'password_reset'.# new
'article'.'userprofile',]...Copy the code
Add the address of your app to the root route /my_blog/urls.py:
/my_blog/urls.py
...
urlpatterns = [
...
path('password-reset/', include('password_reset.urls')),]Copy the code
Modify/templates/userprofile/login. HTML, provide an entrance to reset your password:
/templates/userprofile/login.html
...
<div class="col-12">.<form method="post" action=".">.</form>
<! -- -- -- > new
<br>
<h5>Forgot your password?</h5>
<h5>Click on the<a href='{% url "password_reset_recover" %}'>here</a>To reset your password</h5>
</div>.Copy the code
Emails cannot be created out of thin air. So far, we have not configured the account password of the sender email, nor configured the port of sending emails, the sender and other information.
/my_blog/settings.py also needs to be configured for the sending mailbox:
/my_blog/settings.py
...
# SMTP server, change your mailbox to SMTP
EMAIL_HOST = 'smtp.qq.com'
# mailbox name
EMAIL_HOST_USER = '[email protected]'
# Email password
EMAIL_HOST_PASSWORD = 'Your email password'
# Port to send mail
EMAIL_PORT = 25
Whether to use TLS
EMAIL_USE_TLS = True
The default sender
DEFAULT_FROM_EMAIL = 'Dusay's personal website
'
@foxmail.com>
Copy the code
Simple Mail Transfer Protocol (SMTP) is a Protocol standard used to transmit Email over the Internet.
SMTP is a text-based protocol. On which one or more recipients of a message are specified, the message text is then transmitted. SMTP uses TCP port 25.
SMTP is a “push” protocol (sending mail) that does not allow “pull” messages (receiving mail) from remote servers. To receive mail, the client must use POP3 or IMAP.
Once set up, you can start server testing.
Click the login page:
Click on the link “Here” on the last line:
Enter user name or Email as required and click ok:
The link to reset the password has been sent to the mailbox.
Go to Email to view new messages:
Amazingly, I got an email! Click on the link in the email:
Enter a new password as prompted:
The password reset is successful.
There was no problem trying to log in with the new password, and it worked.
There is no space to overwrite the original template file one by one. If you are interested, peruse the official documentation and try to adapt the template file to match your site’s style.
The official document is here: docs
Making: django – password reset
I’m sure you’ll get a taste of the benefits of using tripartite libraries: ** You can accomplish a lot of functionality with very little code. ** I recommend the use of excellent tripartite libraries in development, which can greatly improve efficiency and reduce repetitive labor. Of course, there are also some disadvantages of using tripartite libraries, such as maintenance difficulties and customization. Whether to use it or not in practice depends on the actual situation.
In the future, more three-party libraries will be introduced, or carry out the same sentence: success is standing on the shoulders of giants.
Common mistakes
Sending emails can cause all kinds of strange problems because it involves setting and permissions for sending emails.
Let’s say your email setting is [email protected]. The project code is correct, but Sina banned SMTP service, that mail will not be sent successfully. If an error occurs, try the following methods:
- Example Set the sending mailbox to allow the SMTP service
- Check whether the account and password are correct
- Some sending ports require additional Settings. Try changing ports
- Change the email address of another service provider
If not, please search for a solution to a similar problem according to the error page.
conclusion
In this chapter, you learned how to use the third-party library Django-password-reset to efficiently reset passwords.
The next chapter learns about extending and updating user profiles.
- If you have any questions please leave a message on Doucet’s personal website and I will reply as soon as possible.
- Or Email me a private message: [email protected]
- Project code: Django_blog_tutorial
Please inform the author and indicate the source for reprinting.