Home > Enterprise >  How to change default password reset email in djoser
How to change default password reset email in djoser

Time:09-26

I want to change the default email template of djoser for sending password reset email, I saw one answer in stackoverflow for changing the activation mail but i don't know how to use that for password reset email

code to change default email for activation: base/reset_email.py

from djoser import email

class ActivationEmail(email.ActivationEmail):
     template_name = 'base/resetPasswordMail.html'

settings.py

DJOSER = {

   'EMAIL': {
   'activation': 'base.reset_email.ActivationEmail'
    },
}

how to replace this code for password reset functionality

CodePudding user response:

The default email configs of djoser are defined as:

'EMAIL': {
    'activation': 'djoser.email.ActivationEmail',
    'confirmation': 'djoser.email.ConfirmationEmail',
    'password_reset': 'djoser.email.PasswordResetEmail',
    'password_changed_confirmation': 'djoser.email.PasswordChangedConfirmationEmail',
    'username_changed_confirmation': 'djoser.email.UsernameChangedConfirmationEmail',
    'username_reset': 'djoser.email.UsernameResetEmail',
}

You want to change the password reset view so you need to make:

DJOSER = {

   'EMAIL': {
   'password_reset': 'appName.viewFileName.PasswordResetEmail'
    },
}

In your view you can define a PasswordResetEmail class. The below code is taken from djoser repository:

class PasswordResetEmail(BaseEmailMessage):
    template_name = "email/password_reset.html"

    def get_context_data(self):
        # PasswordResetEmail can be deleted
        context = super().get_context_data()

        user = context.get("user")
        context["uid"] = utils.encode_uid(user.pk)
        context["token"] = default_token_generator.make_token(user)
        context["url"] = settings.PASSWORD_RESET_CONFIRM_URL.format(**context)
        return context

You can customize this class according to your purpose.

  • Related