39. Resetting password, timezone, and language

Resetting pasword

The "Forgot Password" button within the login view currently lacks functionality. To integrate this feature efficiently without creating multiple URLs, we will leverage Django's built-in password reset mechanisms. This approach allows us to implement the required functionality using Django's robust, pre-built views and forms designed specifically for password recovery processes. The implementation will follow the guidelines and code provided in the official Django documentation.

Django oficial documentation site
Django oficial github repository

Below are the code snippets we will integrate to enable the password reset functionality:

path("password_change/", views.PasswordChangeView.as_view(), name="password_change"),
path("password_change/done/", views.PasswordChangeDoneView.as_view(), name="password_change_done"),
path("password_reset/", views.PasswordResetView.as_view(), name="password_reset"),
path("password_reset/done/", views.PasswordResetDoneView.as_view(), name="password_reset_done"),
path("reset/<uidb64>/<token>/", views.PasswordResetConfirmView.as_view(), name="password_reset_confirm"),
path("reset/done/", views.PasswordResetCompleteView.as_view(), name="password_reset_complete"),

Inside the urls.py file, we will import Django's built-in views to handle all password change-related functionalities.

urls.py
from django.contrib.auth import views

Next, we will add a new path to the urlpatterns list. This path will utilize the previously imported views, specifically calling PasswordChangeView to serve as the view for our website's password change functionality.

path('password_change/', views.PasswordChangeView.as_view(), name="password_change"),
    path('password_change/', include('django.contrib.auth.urls')),

Note: Alternatively, we could have included all the views within the path using the include function, as done earlier in the project. However, this approach would also include unwanted views, such as the login view, which we have already implemented separately. To avoid redundancy, we will not use the include function in this case.

Now, we will also include the additional password-related views provided by Django into the urlpatterns list.

urls.py\urlpatterns
    path("password_change/", views.PasswordChangeView.as_view(),     name="password_change"),
    path("password_change/done/", views.PasswordChangeDoneView.as_view(), name="password_change_done"),
    
    path("password_reset/", views.PasswordResetView.as_view(), name="password_reset"),
    path("password_reset/done/", views.PasswordResetDoneView.as_view(), name="password_reset_done"),
    path("reset/<uidb64>/<token>/", views.PasswordResetConfirmView.as_view(), name="password_reset_confirm"),
    path("reset/done/", views.PasswordResetCompleteView.as_view(), name="password_reset_complete"),

The function of each view is detailed below:

  • password_change: Handles the process of changing the password for a logged-in user.

  • password_change_done: Displays a confirmation page indicating that the password has been successfully changed.

  • password_reset: Manages the password reset process for users who are not logged in and have forgotten their passwords.

  • password_reset_done: Displays a notification page confirming that an email has been sent to the user for password reset.

  • password_reset_confirm: Provides a unique link with a token that allows only the specified user to reset their password.

  • password_reset_complete: Notifies the user that their password has been successfully reset.

Inside the login.html file, we will update the 'I forgot my password' link (<a> tag) to reference the URL for the password reset view that we have implemented.

login.html
<a href="{% url 'password_reset' %}">I forgot my password</a>
{% endblock %}

Now, when you click the "Forgot my password" link on the login page, you will be redirected to Django's password reset page.

Note: We will customize the password reset page to match the theme of our website at a later stage.

As part of the password reset process, the website will send a link to the user's email. To enable this functionality, we need to add the following line of code at the end of the settings.py file:

EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

This line configures Django to retrieve the email address that the user inputs on the password reset page and display it on the console (currently for testing purposes). Later, we will modify this setup to actually send the email.

For now, when the email is entered and the "Reset Password" button is clicked, the email content will appear on the console.

Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Subject: Password reset on 127.0.0.1:8000
From: webmaster@localhost
To: luzsombradante03@gmail.com
Date: Fri, 23 Aug 2024 11:25:23 -0000
Message-ID: <172441232359.12816.1343427201932954510@Dante_ASUS_2>


You're receiving this email because you requested a password reset for your user account at 127.0.0.1:8000.

Please go to the following page and choose a new password:

http://127.0.0.1:8000/reset/NQ/cc7e2b-eea63e26d15a6acaa8b3a0f63cf057ab/      
Your username, in case you’ve forgotten: luzsombradante03@gmail.com

Thanks for using our site!

The 127.0.0.1:8000 team

Important Note: The email will only be generated if the email address provided is associated with an existing account.

The email includes a link to a page where you can reset your password. Clicking this link will redirect you to the password reset page, where you can successfully change your password.

Changing language and timezone

To change the language and timezone of your Django project, navigate to the settings.py file and locate the Internationalization section.

settings.py
LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Sao_Paulo'

In this section, you can modify the LANGUAGE_CODE and TIME_ZONE settings to suit your needs. The default time zone is set to UTC, so I adjusted it to match my local region.

Last updated