【问题标题】:How to customize django rest auth email context如何自定义 django rest auth 电子邮件上下文
【发布时间】:2020-05-29 05:29:51
【问题描述】:

类似于这个问题 How to customize django rest auth password reset email content/template 我想自定义由 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',
        }

但除了自定义模板之外,我还想添加自定义 context。有简单的方法吗?

【问题讨论】:

    标签: django django-rest-auth


    【解决方案1】:

    PasswordResetSerializer 使用来自django.contrib.auth.formsPasswordResetFormPasswordResetFormsave() 方法接受参数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 已在使用中。

    【讨论】:

    • 谢谢,这正是我想要的。
    猜你喜欢
    • 2016-04-26
    • 2017-02-20
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2016-08-10
    • 2018-05-24
    相关资源
    最近更新 更多