用于密码重置的Django-rest-auth HTML电子邮件

时间:2022-08-13 19:21:23

Is it possible to send password reset email in HTML in django-rest-auth? By default, HTML emails are disabled, and send in simple text from password_reset_email.html template. I have copied template to project structure templates, and can easily change the content of email.

是否可以在django-rest-auth中以HTML格式发送密码重置电子邮件?默认情况下,HTML电子邮件被禁用,并从password_reset_email.html模板发送简单文本。我已将模板复制到项目结构模板,并可以轻松更改电子邮件的内容。

But, as documented Django can optionally send fully featured HTML email for password reset. I can't figure it out how to send them from Django-rest-auth view.

但是,据记录,Django可以选择发送功能齐全的HTML电子邮件进行密码重置。我无法弄清楚如何从Django-rest-auth视图发送它们。

Seems like django-rest-auth using custom serializer for def password_reset or class PasswordResetView, both of them have attributes: email_template_name, html_email_template_name.

看起来像django-rest-auth使用自定义序列化程序用于def password_reset或类PasswordResetView,它们都具有属性:email_template_name,html_email_template_name。

It would be great if I can pass this attributes from my urls.py, here it is

如果我可以从我的urls.py传递这个属性,那就太好了

from rest_auth.views import PasswordResetView, 
url(r'^api/auth/reset/$', PasswordResetView.as_view(), name='rest_password_reset'),

Else, I should probably rewrite serializer (or even view) or even view for that.

否则,我应该重写序列化程序(甚至是视图),甚至可以查看。

1 个解决方案

#1


0  

1. Create your own PasswordResetSerializer with customized save method.

1.使用自定义保存方法创建自己的PasswordResetSerializer。

Base PasswordResetSerializer copied from here: (https://github.com/Tivix/django-rest-auth/blob/v0.6.0/rest_auth/serializers.py#L102)

从这里复制Base PasswordResetSerializer:(https://github.com/Tivix/django-rest-auth/blob/v0.6.0/rest_auth/serializers.py#L102)

yourproject_app/serializers.py

from django.contrib.auth.forms import PasswordResetForm
from django.conf import settings
from django.utils.translation import gettext as _
from rest_framework import serializers

###### IMPORT YOUR USER MODEL ######
from .models import ExampleUserModel

class PasswordResetSerializer(serializers.Serializer):
    email = serializers.EmailField()
    password_reset_form_class = PasswordResetForm
    def validate_email(self, value):
        self.reset_form = self.password_reset_form_class(data=self.initial_data)
        if not self.reset_form.is_valid():
            raise serializers.ValidationError(_('Error'))

        ###### FILTER YOUR USER MODEL ######
        if not ExampleUserModel.objects.filter(email=value).exists():

            raise serializers.ValidationError(_('Invalid e-mail address'))
        return value

    def save(self):
        request = self.context.get('request')
        opts = {
            'use_https': request.is_secure(),
            'from_email': getattr(settings, 'DEFAULT_FROM_EMAIL'),

            ###### USE YOUR HTML FILE ######
            'html_email_template_name': 'example_message.html',

            'request': request,
        }
        self.reset_form.save(**opts)

2. Connect custom PasswordResetSerializer to override default

2.连接自定义PasswordResetSerializer以覆盖默认值

yourproject_app/settings.py

REST_AUTH_SERIALIZERS = {
    'PASSWORD_RESET_SERIALIZER': 
        'yourproject_app.serializers.PasswordResetSerializer',
}

3. Add the path to the directory where your custom email message text file is located to TEMPLATES

3.将自定义电子邮件消息文本文件所在目录的路径添加到TEMPLATES

yourproject/settings.py

TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'yourproject/templates')],
        ...
    }
]

4. Write custom email message (default copied from Django)

4.编写自定义电子邮件消息(默认从Django复制)

yourproject/templates/example_message.html

<html>
  <body>
    ...
  </body>
</html>

#1


0  

1. Create your own PasswordResetSerializer with customized save method.

1.使用自定义保存方法创建自己的PasswordResetSerializer。

Base PasswordResetSerializer copied from here: (https://github.com/Tivix/django-rest-auth/blob/v0.6.0/rest_auth/serializers.py#L102)

从这里复制Base PasswordResetSerializer:(https://github.com/Tivix/django-rest-auth/blob/v0.6.0/rest_auth/serializers.py#L102)

yourproject_app/serializers.py

from django.contrib.auth.forms import PasswordResetForm
from django.conf import settings
from django.utils.translation import gettext as _
from rest_framework import serializers

###### IMPORT YOUR USER MODEL ######
from .models import ExampleUserModel

class PasswordResetSerializer(serializers.Serializer):
    email = serializers.EmailField()
    password_reset_form_class = PasswordResetForm
    def validate_email(self, value):
        self.reset_form = self.password_reset_form_class(data=self.initial_data)
        if not self.reset_form.is_valid():
            raise serializers.ValidationError(_('Error'))

        ###### FILTER YOUR USER MODEL ######
        if not ExampleUserModel.objects.filter(email=value).exists():

            raise serializers.ValidationError(_('Invalid e-mail address'))
        return value

    def save(self):
        request = self.context.get('request')
        opts = {
            'use_https': request.is_secure(),
            'from_email': getattr(settings, 'DEFAULT_FROM_EMAIL'),

            ###### USE YOUR HTML FILE ######
            'html_email_template_name': 'example_message.html',

            'request': request,
        }
        self.reset_form.save(**opts)

2. Connect custom PasswordResetSerializer to override default

2.连接自定义PasswordResetSerializer以覆盖默认值

yourproject_app/settings.py

REST_AUTH_SERIALIZERS = {
    'PASSWORD_RESET_SERIALIZER': 
        'yourproject_app.serializers.PasswordResetSerializer',
}

3. Add the path to the directory where your custom email message text file is located to TEMPLATES

3.将自定义电子邮件消息文本文件所在目录的路径添加到TEMPLATES

yourproject/settings.py

TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'yourproject/templates')],
        ...
    }
]

4. Write custom email message (default copied from Django)

4.编写自定义电子邮件消息(默认从Django复制)

yourproject/templates/example_message.html

<html>
  <body>
    ...
  </body>
</html>