【问题标题】:Cutomize dj_rest_auth Password reset email自定义 dj_rest_auth 密码重置电子邮件
【发布时间】:2022-03-14 20:05:12
【问题描述】:

当用户请求重置密码时,我想发送自定义电子邮件。我在 django 中使用 dj_rest_auth。以下是我所做的: 1. 定义了一个继承自 dj_rest_auth 的 PasswordResetSerializer 的自定义序列化器

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

然后在settings.py中指向这个序列化器:

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

然后在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',
        ],
    },
},

]

然后我在项目根目录下创建了 templates 文件夹,在其中创建了一个 registration 文件夹,并将 password_reset_email.html 放入其中。

这是我在谷歌搜索一段时间后发现的问题的确切解决方案,但这对我不起作用。我错过了什么?

【问题讨论】:

    标签: django django-rest-framework django-allauth dj-rest-auth


    【解决方案1】:

    这个答案帮助了我 https://stackoverflow.com/a/70624462/15624533

    我只是将我的文件(html 或 txt)直接放入 templates 文件夹,并将 'account/email/password_reset_key' 更改为 'password_reset_key em>' 在 send_mail 方法中。

    【讨论】:

      【解决方案2】:

      我遇到了同样的挑战,并从 GitHub 上的这个问题中得到了解决方案。 https://github.com/iMerica/dj-rest-auth/issues/9 。比你想象的要容易

      创建您的自定义密码重置序列化程序

      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)
      

      如果您只想自定义电子邮件参数,您可以忽略覆盖 save() 方法并覆盖 get_email_options() 来代替

      from dj_rest_auth.serializers import PasswordResetSerializer
      
      class MyPasswordResetSerializer(PasswordResetSerializer):
      
          def get_email_options(self) :
            
              return {
                  'email_template_name': 'password_reset_email.html'
              }
      

      然后在 settings.py 中指向您的自定义序列化程序

      REST_AUTH_SERIALIZERS = {
          'PASSWORD_RESET_SERIALIZER': 'path.to.your.CustomPasswordResetSerializer'
      }
      

      【讨论】:

        猜你喜欢
        • 2021-05-15
        • 2022-07-06
        • 2016-04-29
        • 1970-01-01
        • 2021-02-07
        • 2021-02-01
        • 1970-01-01
        • 2017-02-26
        • 1970-01-01
        相关资源
        最近更新 更多