这一行代码负责发送包含密码重置链接的电子邮件。
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的自定义模板的正确方法是什么?
发布于 2021-03-07 05:56:36
在django PasswordResetView中使用自定义电子邮件模板之前,您需要知道以下几点。
registration/password_reset_email.html作为电子邮件内容的默认文件,用于重新设置密码,除非您在PasswordResetView的html_email_template_name param值中显式地定义/提供它。下面是通过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 )也可以用于电子邮件模板。
有用资源:
PasswordResetVieww的工作原理.https://stackoverflow.com/questions/66501864
复制相似问题