【问题标题】:How to change the template of password-reset-email properly Django如何正确更改密码重置电子邮件的模板 Django
【发布时间】:2021-06-04 16:25:14
【问题描述】:

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

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'),
...

问题是邮件里面的reset-link由一个uidb64值和一个token组成,比如:

localhost:8000/password-reset/calculated_uidb64/calculated_token

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

【问题讨论】:

  • @HemantMalik 非常感谢。我实际上对你分享的话题有评论。问题是我不知道如何将必要的参数传递到模板中。 django 是否计算整个链接并且我以某种方式将该链接传递给模板?或者我应该使用 uidb64 和令牌从头开始形成链接?如果有怎么办?
  • 我刚刚找到你的评论。你试过这个答案stackoverflow.com/a/56530555/10140011
  • @HemantMalik 我不明白 .txt 文件的功能。另外我不知道如何在电子邮件模板中设置和定位重置链接。如何传递链接参数?
  • 在上面的共享答案中,建议在模板中添加<a href="{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}" style=".."> Reset Password </a> 以添加url,您是否尝试过这个。另外,我发现一篇文章证明了上面分享的答案。看看garmoncheg.blogspot.com/2012/07/…

标签: python html django email templates


【解决方案1】:

在 django PasswordResetView 中使用自定义电子邮件模板之前,您需要了解以下几点。

  1. django 使用registration/password_reset_email.html 作为密码重置电子邮件内容的默认文件,除非您在PasswordResetViewhtml_email_template_name 参数值中明确定义/提供它。

  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是一个用户模型实例,其他值如user.id,user.contact_number也可以在email模板中使用。

有用的资源:

  1. 我强烈推荐这个article 地址Django: Resetting Passwords (with internal tools)
  2. Official django documentation 描述有用的选项和上下文。
  3. Code in django repository 深入了解PasswordResetVieww 的工作原理。

【讨论】:

  • 哇,我也在寻找上下文标签。答案提供的比我要求的要多。很高兴认识能够理解对方并提供不问的人。非常感谢。祝你一切顺利。
猜你喜欢
  • 2012-05-16
  • 2015-10-18
  • 2017-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多