如何自定义Django Rest Auth电子邮件上下文

3

类似于这个问题 如何自定义Django rest auth重置密码电子邮件内容/模板 我想要自定义由Django rest auth自动发送的密码重置(和其他)电子邮件。可以使用自定义电子邮件模板和自定义序列化程序来实现完美的工作:

class CustomPasswordResetSerializer(PasswordResetSerializer):
    def get_email_options(self):
        return {
            'domain_override': settings.FRONTEND_URL,
            'email_template_name': 'registration/custom_reset_email.txt',
            'html_email_template_name': 'registration/custom_reset_email.html',
        }

除了自定义模板外,我还想添加自定义上下文。有简单的方法可以做到吗?

1个回答

4
< p > PasswordResetSerializer 使用来自 django.contrib.auth.forms PasswordResetForm PasswordResetForm save() 方法接受参数 extra_email_context 。 因此,您只需要将 extra_email_context 添加到您返回的字典中即可:

def get_email_options(self):
    extra_context = {...}  # your extra context parameters
    return {
            'domain_override': settings.FRONTEND_URL,
            'email_template_name': 'registration/custom_reset_email.txt',
            'html_email_template_name': 'registration/custom_reset_email.html',
            'extra_email_context': extra_context
        } 

请确保您的extra_context不会覆盖现有的键:emailtokendomainsite_nameuseruidprotocol已经被使用。


谢谢,这正是我在寻找的。 - Manuel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接