0

I have customized my Django reset password template but every time I send reset password emails they are sent as html which hasn't been converted into a string A good example is this

        <p>please go to the following page and choose a new password:</p>
        <h5>
        http://127.0.0.1:8000/api/accounts/reset/MQ/52b-204bbaf9b94c438dff7e/
        Thanks for using our site!
        </h5>

This is how it appears my inbox. My code is follows: urls.py

urlpatterns = [
    path('signup', UserCreate.as_view(), name='signup'),
    path('login', UserLoginAPIView.as_view(), name='login'),
    re_path(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
            activate, name='activate'),
    path('password/reset', PasswordResetView.as_view(),
         {'template_name': 'templates/registration/password_reset_email.html',
          'html_email_template_name': 'templates/registration/password_reset_email.html',
          'subject_template_name': 'templates/registration/password_reset_subject.txt',
          'from_email': config('EMAIL_HOST_USER'),
          'extra_email_context': 'templates/registration/password_reset_email.txt',
          },
         name='password_reset'),
    path('password/reset/done', PasswordResetDoneView.as_view(),
         {'template_name': 'templates/registration/password_reset_done.html'},
         name='password_reset_done'),
    re_path(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>.+)/$', PasswordResetConfirmView.as_view(),
            name='password_reset_confirm'),
    path('reset/done/', PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]

password_reset.html

<p>please go to the following page and choose a new password:</p>
<h5>
http://127.0.0.1:8000{% url 'password_reset_confirm' uidb64=uid token=token %}
Thanks for using our site!
</h5>

I have done research on how I can convert html to a string with no success

toel
  • 155
  • 2
  • 15

1 Answers1

0

Django's default email system has the ability to send emails as either text or html. You're probably sending via the text option only.

Check out EmailMultiAlternatives as described in this SO post.

Also look at the docs for the full explanation.

Edit:

from the Docs, to send an html email:

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

Notice the the "msg.attach_alternative(html_content, "text/html")

JHRS
  • 367
  • 4
  • 11
  • I think that it's supposed to be converted by Django backend which is sending the email? I really don't understand why its not doing that – toel Dec 21 '18 at 16:59
  • Because your most likely telling Django to send the email as text instead of html. I'll edit my answer, but you should read the Docs in the link I sent you. – JHRS Dec 21 '18 at 18:38