【问题标题】:Django generated email doesn't render properlyDjango 生成的电子邮件无法正确呈现
【发布时间】:2016-02-17 21:18:10
【问题描述】:

我正在尝试修改 django-registration 中的密码表格以包含超链接。我对activation_email.html 使用了完全相同的代码,但它不适用于password_reset_email.html。生成的电子邮件正在打印所有 html 标签,如 。我错过了什么?

下面是password_reset_email.html的代码:

{% load i18n %}
<!doctype html>
<html lang="en">

{% blocktrans %}Hello,{% endblocktrans %}

{% blocktrans %}
To reset your password, please click the following link:
{% endblocktrans %}
<body>
    <p> 
        <a href="http://{{site.domain}}{% url 'auth_password_reset_confirm' uid token %}">
            Reset password
        </a>
    </p>
</body>
{% blocktrans %}
If you do not wish to reset your password, please ignore this message.
{% endblocktrans %}

{% blocktrans %}Thanks,{% endblocktrans %}
{% blocktrans %}Management Team{% endblocktrans %}


{# This is used by django.contrib.auth #}

这里是用于比较的activation_email.html 的代码。这会产生正确的结果。

{% load i18n %}
<!doctype html>
<html lang="en">

<head>
    <title>{{ site.name }} {% trans "registration" %}</title>
</head>

<body>
<p>
    {% blocktrans with site_name=site.name %}
    Hello!
</p>

<p>
    We're ready to activate your account. All we need to do is make sure this is your email address.
    {% endblocktrans %}
</p>    

<p> 
    <a href="http://{{site.domain}}{% url 'registration_activate' activation_key %}">
        Confirm email
    </a>
</p>
<p>
    {% blocktrans %}
    If you didn't create a account, please just delete this email.
    {% endblocktrans %}

</p>
</body>

</html>

【问题讨论】:

    标签: python html django django-forms


    【解决方案1】:

    听起来电子邮件是以文本形式发送的。确保您使用的任何方法都以 html 格式发送。

    如果您使用django.core.mail 中的send_mail,则除了message 之外,您还需要提供html_message 参数。

    【讨论】:

    • 我认为 django-registration 可能使用了 django 默认密码模板。 (再次说明 form.email 如何工作的文档)。有什么地方我应该指定这个吗?
    • 我明白了你的意思。我将我的网址更改为此。这是你想要的吗? url(r'^password/reset/$', auth_views.password_reset, {'html_email_template_name': 'registration/password_reset_email.html', 'post_reset_redirect': reverse_lazy('auth_password_reset_done')}, name='auth_password_reset'),跨度>
    • 现在 html 电子邮件没有收到 {{site.domain}}。你知道如何解决这个问题吗?
    • 实际上看起来 django-registration 默认不支持 html 电子邮件:django-registration.readthedocs.org/en/master/… 您可能需要修改它的模型,如这个答案:stackoverflow.com/questions/1325983/… site.domain 可能需要明确在视图中设置并传递给模板,如果它没有拾取它。您可以扩展任何内置视图来做到这一点。
    • 非常感谢您的帮助。奇怪的是,激活电子邮件已经作为 html 发送(因为我的 html 用于上面的激活工作)。但是,密码重置似乎使用了一些我看不到文档的默认 django 模板。在这种情况下我该怎么办?我没有定义可以修改的视图或表单。
    猜你喜欢
    • 2022-12-09
    • 1970-01-01
    • 2015-10-22
    • 2020-01-06
    • 2013-08-09
    • 2018-04-18
    • 2020-08-01
    • 1970-01-01
    • 2022-01-03
    相关资源
    最近更新 更多