【问题标题】:Password reset not redirecting to the template in Django密码重置未重定向到 Django 中的模板
【发布时间】:2018-08-09 01:59:38
【问题描述】:

我正在尝试使用 html 模板覆盖默认电子邮件以在 Django rest-auth 中重置密码。

我正在尝试使用:

from django.conf.urls import url
from django.contrib.auth import views as auth_views

urlpatterns = [
    url(r'^password/reset/', auth_views.password_reset,
    {
        'html_email_template_name':'registration/password_reset_email_html.html',
    }, 
    name="password_reset"),
    url('^', include('django.contrib.auth.urls')),
]

电子邮件已成功发送,但使用默认模板。 我可以通过添加自定义模板文件registration/password_reset_email.html 来覆盖它,但随后电子邮件仅以纯文本形式发送。

我在这里做错了什么?

【问题讨论】:

    标签: django django-registration django-rest-auth


    【解决方案1】:

    我们之前也遇到过这种情况,我们使用 django.core.mail 中的 EmailMultiAlternatives 来发送 html 电子邮件。

    这是一个示例代码:

    def custom_send_mail(to_email,subject,html_email_template_name=None,context=None,from_email = None):
    # Email subject *must not* contain newlines
    subject = ''.join(subject.splitlines())
    email_message = EmailMultiAlternatives(subject, subject, from_email, [to_email])
    #html_email_template_name = "registration/password_reset_confirm_html_email.html"
    if html_email_template_name is not None:
        html_email = loader.render_to_string(html_email_template_name,context)
        email_message.attach_alternative(html_email, 'text/html')
    
    email_message.send()
    

    【讨论】:

      【解决方案2】:
      url(r'^password/reset/$',auth_views.password_reset,{'template_name':'registration/password_reset_email_html.html'},name='reset_password'),
      

      按照上面的 urlpattern 操作可能会有所帮助。 另请参阅:https://simpleisbetterthancomplex.com/tutorial/2016/09/19/how-to-create-password-reset-view.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-15
        • 2014-07-22
        • 1970-01-01
        • 1970-01-01
        • 2017-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多