如何自定义django rest auth电子邮件上下文

问题描述 投票:1回答:1

类似于这个问题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个回答
0
投票

PasswordResetSerializer使用PasswordResetForm中的django.contrib.auth.formssave()PasswordResetForm方法接受参数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已经在使用中。

© www.soinside.com 2019 - 2024. All rights reserved.