3
votes

Is it possible to send password reset email in HTML in django-rest-auth? By default, HTML emails are disabled, and send in simple text from password_reset_email.html template. I have copied template to project structure templates, and can easily change the content of email.

But, as documented Django can optionally send fully featured HTML email for password reset. I can't figure it out how to send them from Django-rest-auth view.

Seems like django-rest-auth using custom serializer for def password_reset or class PasswordResetView, both of them have attributes: email_template_name, html_email_template_name.

It would be great if I can pass this attributes from my urls.py, here it is

from rest_auth.views import PasswordResetView, 
url(r'^api/auth/reset/$', PasswordResetView.as_view(), name='rest_password_reset'),

Else, I should probably rewrite serializer (or even view) or even view for that.

2

2 Answers

1
votes

1. Create your own PasswordResetSerializer with customized save method.

Base PasswordResetSerializer copied from here: (https://github.com/Tivix/django-rest-auth/blob/v0.6.0/rest_auth/serializers.py#L102)

yourproject_app/serializers.py

from django.contrib.auth.forms import PasswordResetForm
from django.conf import settings
from django.utils.translation import gettext as _
from rest_framework import serializers

###### IMPORT YOUR USER MODEL ######
from .models import ExampleUserModel

class PasswordResetSerializer(serializers.Serializer):
    email = serializers.EmailField()
    password_reset_form_class = PasswordResetForm
    def validate_email(self, value):
        self.reset_form = self.password_reset_form_class(data=self.initial_data)
        if not self.reset_form.is_valid():
            raise serializers.ValidationError(_('Error'))

        ###### FILTER YOUR USER MODEL ######
        if not ExampleUserModel.objects.filter(email=value).exists():

            raise serializers.ValidationError(_('Invalid e-mail address'))
        return value

    def save(self):
        request = self.context.get('request')
        opts = {
            'use_https': request.is_secure(),
            'from_email': getattr(settings, 'DEFAULT_FROM_EMAIL'),

            ###### USE YOUR HTML FILE ######
            'html_email_template_name': 'example_message.html',

            'request': request,
        }
        self.reset_form.save(**opts)

2. Connect custom PasswordResetSerializer to override default

yourproject_app/settings.py

REST_AUTH_SERIALIZERS = {
    'PASSWORD_RESET_SERIALIZER': 
        'yourproject_app.serializers.PasswordResetSerializer',
}

3. Add the path to the directory where your custom email message text file is located to TEMPLATES

yourproject/settings.py

TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'yourproject/templates')],
        ...
    }
]

4. Write custom email message (default copied from Django)

yourproject/templates/example_message.html

<html>
  <body>
    ...
  </body>
</html>
0
votes

The simplest solution is:

  1. First:

Create this directory in your project folder,

myProjectName/templates/registration

  1. Second:

go to this directory (in this case I'm using Python 3.6),

"C:\Python36\Lib\site-packages\django\contrib\admin\templates\registration"

copy "password_reset_email.html" file to the directory that you created in the first step.

  1. Third:

go to "setting.py" file in your project and tell django where to find the "password_reset_email.html" file by adding the templates directory:

TEMPLATES = [
{
    ...
    'DIRS': [os.path.join(BASE_DIR, 'myProjectName/templates')],
    ...
}

]

Now you can open "password_reset_email.html" in your project directory and modify it as you wish.