0

当用户请求重置密码时,我想发送自定义电子邮件。我在 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',
        ],
    },
},

]

然后我在项目根目录中创建了模板文件夹,在其中创建了一个注册文件夹,并在其中放置了password_reset_email.html

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

4

1 回答 1

0

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

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

于 2022-01-29T18:02:12.143 回答