1

I want to send customized emails when a user request a password reset. I am using dj_rest_auth with django. Here is what I have done: 1. Defined a custom serializer that inherits from PasswordResetSerializer of dj_rest_auth

class CustomPasswordResetSerializer(PasswordResetSerializer):
    def get_email_options(self):
        return {
            'html_mail_template_name': 'registration/password_reset_email.html',
        }

Then in settings.py pointed to this serializer:

REST_AUTH_SERIALIZERS = {
    'LOGIN_SERIALIZER': 'users.serializers.CustomLoginSerializer',
    'PASSWORD_RESET_SERIALIZER': 'users.serializers.CustomPasswordResetSerializer',
}

Then configured templates in settings.py

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},

]

Then I created templates folder in project root, created a registration folder inside it and placed password_reset_email.html inside it.

This is what I found as an exact solution formy problem after googling some time,but this is not working for me. What did I miss?

Qeybulla
  • 122
  • 1
  • 8

2 Answers2

1

This answer helped me https://stackoverflow.com/a/70624462/15624533

I just put my files (html or txt) directly to templates folder, and changed 'account/email/password_reset_key' to just 'password_reset_key' in send_mail method.

Qeybulla
  • 122
  • 1
  • 8
1

I faced the same challenge and got the solution from this issue on GitHub. https://github.com/iMerica/dj-rest-auth/issues/9 . It's easier than you think

Create your custom password reset serializer

from dj_rest_auth.serializers import PasswordResetSerializer

class CustomPasswordResetSerializer(PasswordResetSerializer):
    def save(self):
        request = self.context.get('request')
        # Set some values to trigger the send_email method.
        opts = {
            'use_https': request.is_secure(),
            'from_email': 'example@yourdomain.com',
            'request': request,
            # here I have set my desired template to be used
            # don't forget to add your templates directory in settings to be found
            'email_template_name': 'password_reset_email.html'
        }

        opts.update(self.get_email_options())
        self.reset_form.save(**opts)

If you only want to customize email parameters nothing more or less, you can ignore overriding save() method and override get_email_options() instead

from dj_rest_auth.serializers import PasswordResetSerializer

class MyPasswordResetSerializer(PasswordResetSerializer):

    def get_email_options(self) :
      
        return {
            'email_template_name': 'password_reset_email.html'
        }

then point to your custom serializer in settings.py

REST_AUTH_SERIALIZERS = {
    'PASSWORD_RESET_SERIALIZER': 'path.to.your.CustomPasswordResetSerializer'
}
K.Nehe
  • 424
  • 10
  • 22