dj rest auth 重置密码在电子邮件中发送原始 html

dj rest auth reset password sends raw html in email

我使用 dj rest auth 包,我已经像这样覆盖了重置密码电子邮件

在 settings.py 中:

REST_AUTH_SERIALIZERS = {
'JWT_TOKEN_CLAIMS_SERIALIZER': 'account.token.TokenObtainPairSerializer',
'USER_DETAILS_SERIALIZER': 'account_profile.api.userupdate.UserSerializer',
'PASSWORD_RESET_SERIALIZER': 'account_profile.api.customemail.CustomPasswordResetSerializer'}

并在 customemail.py 中:

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': 'noreply@mizbans.com',
        'from_email': getattr(settings, 'DEFAULT_FROM_EMAIL'),
        '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)

并在 email.py 中:

class MyPasswordResetSerializer(PasswordResetSerializer):
context={'domain':settings.WEBSITE_DOMAIN,'site_name':'mizban'}

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

我在 password_reset_email 中放置了示例 html 代码:

 <html>
    <body>
      <p>welcome to{{domain}}</p>
      <p>site {{site_name}}</p>
    </body>
</html>

它有效,但它也会在电子邮件模板中发送 html 标记代码!

我通过添加这个解决了这个错误:

'html_email_template_name': 'password_reset_email.html'

在保存方法中选择={}