Sandeep Kushwaha
Sandeep Kushwaha

Reputation: 25

password_reset email template doesn't render html tag

I'm trying to override content for default password_reset_email.html with some html tag but it doesn't work. Here is my modification. For testing purpose, I'm just using p tag. But i doesn't work. I'm totally unaware what going wrong.

Here is my process.

urls.py

path('accounts/', include('django.contrib.auth.urls')),

and here is my html templates

{% load i18n %}
{% autoescape on %}
{% blocktrans %}You're receiving this email because you requested a password reset for your user account at
{{ site_name }}.{% endblocktrans %}

{% trans "Please go to the following page and choose a new password:" %}
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}
{% endblock %}
<p>Hello world</p>
{% trans 'Your username, in case you’ve forgotten:' %} {{ user.get_username }}

{% trans "Thanks for using our site!" %}

{% blocktrans %}The {{ site_name }} team{% endblocktrans %}

{% endautoescape %}

and here is the output

enter image description here

Upvotes: 0

Views: 865

Answers (2)

yusuf khan
yusuf khan

Reputation: 1

This is just 5 letters to be added, and boom..!! you work will done.

change---

urls.py

patterns = [
path('accounts/password_reset/',
    views.PasswordResetView.as_view(
        email_template_name='registration/password_reset_email.html'
    ),
    name='password_reset'
),
path('accounts/', include('django.contrib.auth.urls')),
# …]

to--

patterns = [
path('accounts/password_reset/',
    views.PasswordResetView.as_view(
        html_email_template_name='registration/password_reset_email.html'
    ),
    name='password_reset'
),
path('accounts/', include('django.contrib.auth.urls')),
# …]

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476614

The email_template_name is standard one that is send in raw text, not in HTML. Indeed, in the PasswordResetView we see:

class PasswordResetView(PasswordContextMixin, FormView):
    email_template_name = 'registration/password_reset_email.html'
    extra_email_context = None
    form_class = PasswordResetForm
    from_email = None
    html_email_template_name = None
    subject_template_name = 'registration/password_reset_subject.txt'
    success_url = reverse_lazy('password_reset_done')
    template_name = 'registration/password_reset_form.html'
    title = _('Password reset')
    token_generator = default_token_generator

    @method_decorator(csrf_protect)
    def dispatch(self, *args, **kwargs):
        return super().dispatch(*args, **kwargs)

    def form_valid(self, form):
        opts = {
            'use_https': self.request.is_secure(),
            'token_generator': self.token_generator,
            'from_email': self.from_email,
            'email_template_name': self.email_template_name,
            'subject_template_name': self.subject_template_name,
            'request': self.request,
            'html_email_template_name': self.html_email_template_name,
            'extra_email_context': self.extra_email_context,
        }
        form.save(**opts)
        return super().form_valid(form)

In case you thus alter the registration/password_reset_email.html, it will still send the email as raw text, so an email client will render it as raw text.

What you can do is simply make a new path that takes precedence, and where you override the html_email_template_name:

# urls.py

urlpatterns = [
    path('accounts/password_reset/',
        views.PasswordResetView.as_view(
            html_email_template_name='registration/password_reset_email.html'
        ),
        name='password_reset'
    ),
    path('accounts/', include('django.contrib.auth.urls')),
    # …
]

It is important that this path appears before the path that includes the django.contrib.auth.urls, since otherwise it will still take the "old" path.

Upvotes: 2

Related Questions