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.
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.
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.
It would be great if I can pass this attributes from my urls.py, here it is
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. Create your own PasswordResetSerializer
with customized save
method.
Base PasswordResetSerializer
copied from here: (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
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
yourproject/settings.py
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'yourproject/templates')],
...
}
]
4. Write custom email message (default copied from Django)
yourproject/templates/example_message.html
<html>
<body>
...
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With