Authentication
Authentication

Django

First I leave you a link to the official documentation:

https://docs.djangoproject.com/en/4.0/topics/auth/default/  the first thing to do is to include the following urlpatterns in our urls.py file:

Documentation
file urls.py

Trying to access accounts we find the list of templates to create shown in the official documentation. I create a templates folder which is at the same level as the apps and configure it in the settings.py file.

file settings.py

I’m going to create the registration folder inside. In this folder we will put all the templates we need for authentication in Django. I create the login.html file. Going to the documentation scrolling down we find a template that we can use.

login.html

Action seems associated with a url that is part of the Django template engine, it allows us to obtain a url based on the name we have assigned to the path. Next is a parameter passed as a hidden field from the view that indicates where the user should be redirected.

login.html
url login

Let’s see the code of base.html.

base.html

Once we have entered the data in the login form we get an error because the redirect produced by next generates an error. We can work around this by going to settings.py and setting the home page as the return address from the login.

file settings.py

Let’s now create the logged_out.html file. It is a very simple file that needs no explanation.

logged_out.html

As for the password reset, the question is a bit more complex as we need five html files. The password_reset_form.html file generates an email and the link to reset the password. Obviously, not having a backend system capable of sending the e-mails, we will use the console to obtain the link through the constant EMAIL_BACKEND present in the settings.py file.

Email-backend

Let’s see the file. Nothing particularly complicated.

Password_reset_form.html

Let’s see the file password_reset_done.html.

password_reset_done.html

The password_reset_email.html file is used to generate the link for changing the password. The recommendation is to specify a valid e-mail for the link to be generated.

password_reset_email.html

The password_reset_confirm.html file is the file where the password is changed, it also checks if the generated link is valid. If it is not valid, an error is generated and the procedure is repeated.

password_reset_confirm.html

Once the password change is completed you are redirected to the password_reset_complete.html template. The procedure ends.

password_reset_complete.html

LINKS TO PREVIOUS POST

PREVIOUS POST LINKS

LINK TO THE CODE ON GITHUB

GITHUB