Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Django Password Reset Email Beautiful HTML?

Tags:

django

My Django application currently is setup to use the native registration package to handle user authentication and management.

I have created a handful of files in myApp/templates/registration that are used to send out the password reset token as described in these docs.

It works fine. Except the password reset email is an ugly text-only monstrosity. I would like to make it match the look and feel of all other emails my application sends. IE: I want it to be HTML and contain images, styles and links. How do I do that?

I followed the detailed instructions here. However, there is an error in that code that I don't know what to do with: 'CustomPasswordResetForm' object has no attribute 'users_cache'

Can someone show me a detailed working example how to accomplish this? I wish it weren't so hard.

like image 311
Saqib Ali Avatar asked Feb 15 '14 05:02

Saqib Ali


People also ask

How can I recover my Django password?

You will need to reset that users password. Try using the set_password(raw_password) method to give the user a new password. Remember to call the save() method to ensure you save the change to the database.

What is a reset password email?

Password reset emails are some of the most succinct emails you can send. Generally speaking, they have one goal: to help users securely re-establish access to their accounts. In most cases, that will be through sending a password reset link.


1 Answers

Adding my findings for django version 2.0 as I found the rest of the answers to this question to be out-of-date.

With 2.0, the proper way of adding a URL to your urls.py file is by using path():

from django.urls import path
from django.contrib.auth import views as auth_views

path('accounts/password_reset/', auth_views.PasswordResetView.as_view(
  html_email_template_name='registration/password_reset_html_email.html'
)),

The next code snippet to highlight here is the .as_view() function. Django 2.0 implements auth views as classes. You can read more about this in the Authentication Views documentation

You then "convert" the class to a view using `.as_view() and you are able to pass in any class attributes defined in the source code as named parameters.

Passing in html_email_template_name (which defaults to None) automatically sends an html email.

You can access the source code for PasswordResetView by following this python path: django.contrib.auth.views

Here you can see the other class attributes you can pass into PasswordResetView and the other auth views. This is super helpful for passing extra_context into your django templates as well.

like image 160
Daniel Long Avatar answered Oct 02 '22 11:10

Daniel Long