如何正确更改Django密码重置电子邮件的模板

5

这行代码负责发送包含密码重置链接的电子邮件。

path('accounts/password-reset/', auth_views.PasswordResetView.as_view(), name='password_reset'),

然而,这封电子邮件看起来完全无聊,很难在阅读时区分重要部分。

为了吸引用户的注意力并更好地指导他们,我想在此电子邮件正文中添加样式。

可以通过以下几行代码将自定义模板添加到电子邮件中:

...
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/password-reset/', auth_views.PasswordResetView.as_view(html_email_template_name='registration/password_reset_email.html'), name='password_reset'),
...

邮件中重置链接由uidb64值和令牌组成,例如:
localhost:8000/password-reset/calculated_uidb64/calculated_token

什么是将这些值传递给自定义模板password_reset_email.html的正确方法?

2
看看这个 https://dev59.com/_2Eh5IYBdhLWcg3w_nuv - Hemant
1
我刚看到了你的评论。你试过这个答案吗?https://dev59.com/_2Eh5IYBdhLWcg3w_nuv#56530555 - Hemant
@ HemantMalik 我不明白 .txt 文件的功能。我也不知道如何在电子邮件模板中设置和定位重置链接。如何传递链接参数? - Xfce4
1
在上面的共享答案中,建议在模板中添加 <a href="{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}" style=".."> 重置密码 </a> 来添加 URL,你试过了吗?此外,我发现一篇文章可以证明上面分享的答案。请看 http://garmoncheg.blogspot.com/2012/07/django-resetting-passwords-with.html - Hemant
@HemantMalik,太好了,你的方法有效。非常感谢。我当时就在你提供的页面上,但是错过了关键信息。实际上,我一开始认为django版本太旧了,所以没有详细阅读,因为django在不同版本之间可能会有巨大的变化。如果你有时间,我很高兴你能把你的评论重新排列成一个实际的答案,这样解决方案对于每个人都会更加明显,特别是那些使用较新版本django的人。 - Xfce4
显示剩余2条评论
1个回答

10
以下是使用自定义电子邮件模板的 Django PasswordResetView 之前需要了解的几个事项:
  1. 除非您在 PasswordResetView 的 html_email_template_name 参数值中明确定义或提供,否则 django 将使用 registration/password_reset_email.html 作为密码重置的默认电子邮件内容文件。
  2. Django 允许在电子邮件模板中使用 jinja 模板,以根据您的要求进行修改。
  3. PasswordResetView 提供了所需的密码重置视图上下文,例如用户实例以填充任何用户详细信息、site_name、token 等。
以下是使用 Django 模板语言通过上下文创建电子邮件模板的示例:
{% autoescape off %}
You're receiving this e-mail because you requested a password reset for your user account at {{ site_name }}.

Please go to the following page and choose a new password:
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url django.contrib.auth.views.password_reset_confirm uidb36=uid, token=token %}
{% endblock %}

Your username, in case you've forgotten: {{ user.username }}

Thanks for using our site!

The {{ site_name }} team.

{% endautoescape %}

上述模板中使用(或可使用)的上下文如下:

email: An alias for user.email
user: The current User, according to the email form field. Only active users are able to reset their passwords (User.is_active is True).
site_name: An alias for site.name. If you don’t have the site framework installed, this will be set to the value of request.META['SERVER_NAME']. For more on sites, see The “sites” framework.
domain: An alias for site.domain. If you don’t have the site framework installed, this will be set to the value of request.get_host().
protocol: http or https
uid: The user’s primary key encoded in base 64.
token: Token to check that the reset link is valid.

注意:作为用户模型实例,其他值如user.id、user.contact_number也可以在电子邮件模板中使用。
有用的资源:
1. 我强烈推荐这篇文章,介绍了Django:使用内部工具重置密码
2. 官方Django文档描述了有用的选项和上下文。
3. Django存储库中的代码,深入了解PasswordResetView的工作原理。

哇,我也在寻找上下文标签。这个答案提供了比我要求的更多。很高兴遇到能够理解对方并无私提供帮助的人。非常感谢。祝你一切顺利。 - Xfce4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接