0

我已经自定义了我的 Django 重置密码模板,但是每次我发送重置密码电子邮件时,它们都以 html 的形式发送,尚未转换为字符串 一个很好的例子是

        <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>

这就是我的收件箱的显示方式。我的代码如下: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>

我已经研究了如何将 html 转换为字符串但没有成功

4

1 回答 1

0

Django 的默认电子邮件系统能够以文本或 html 格式发送电子邮件。您可能仅通过文本选项发送。

查看SO 帖子中描述的 EmailMultiAlternatives。

另请查看文档以获取完整说明。

编辑:

从文档中发送 html 电子邮件:

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()

注意 "msg.attach_alternative(html_content, "text/html")

于 2018-12-21T16:25:41.910 回答