【发布时间】:2020-08-02 22:27:00
【问题描述】:
我正在使用 Django Rest Password Reset 来重置密码工作流程,因为在我看来,它是对这种特殊情况的最佳支持。
在 urls.py 中
# Password reset
path('reset-password/verify-token/', views.CustomPasswordTokenVerificationView.as_view(), name='password_reset_verify_token'),
path('reset-password/', include('django_rest_passwordreset.urls', namespace='password_reset')),
在settings.py中
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
如果我在运行服务器后去重置密码/,这就是我得到的
如果我发布了一封未存储在数据库中的电子邮件,那么它会给出一个 HTTP 400 错误请求
{
"email": [
"There is no active user associated with this e-mail address or the password can not be changed"
]
}
如果我发布存储在数据库中的电子邮件,那么我会收到
TemplateDoesNotExist at /test_app/reset-password/ user_reset_password.html
我将信号接收器放置在名为 CustomPasswordResetView 的视图中
class CustomPasswordResetView:
@receiver(reset_password_token_created)
def password_reset_token_created(sender, reset_password_token, *args, **kwargs):
"""
Handles password reset tokens
When a token is created, an e-mail needs to be sent to the user
"""
# send an e-mail to the user
context = {
'current_user': reset_password_token.user,
'username': reset_password_token.user.username,
'email': reset_password_token.user.email,
'reset_password_url': "{}?token={}".format(reverse('password_reset:reset-password-request'), reset_password_token.key)
}
# render email text
email_html_message = render_to_string('user_reset_password.html', context)
email_plaintext_message = render_to_string('user_reset_password.txt', context)
msg = EmailMultiAlternatives(
# title:
"Password Reset for {title}".format(title="Some website title"),
# message:
email_plaintext_message,
# from:
"test@test.com",
# to:
[reset_password_token.user.email]
)
msg.attach_alternative(email_html_message, "text/html")
msg.send()
正如您在其中看到的那样,有一个用于呈现电子邮件文本的地方
email_html_message = render_to_string('user_reset_password.html', context)
email_plaintext_message = render_to_string('user_reset_password.txt', context)
在视图所在的同一个文件夹中,创建了两个文件 user_reset_password.html 和 user_reset_password.txt,内容相同
{% load i18n %}{% blocktrans %}Hello!
You're receiving this e-mail because you or someone else has requested a password for your user account.
It can be safely ignored if you did not request a password reset. Click the link below to get the token.{% endblocktrans %}
{{ reset_password_url }}
Then, if you go to /test_app/reset-password/confirm/, you can paste the token and the new password.
{% if email %}{% blocktrans %}In case you forgot, your email is {{ email }}.{% endblocktrans %}
{% endif %}{% blocktrans %}Have a great day!
{% endblocktrans %}
为什么我仍然TemplateDoesNotExist at /test_app/reset-password/user_reset_password.html以及如何解决?
【问题讨论】:
-
我有一些来自 user_reset_password.txt 的问题,据我所知,我们必须在电子邮件中提供两个链接。一是生成令牌 一是添加令牌和新密码以重置密码。如果是,我是对的?那么仅通过电子邮件将令牌发送给用户并仅提供重置密码的链接不是更好吗?请告诉我谢谢
-
@sunil 嗨。这不是这个问题的内容,而且您似乎并不关心阅读文档。正如文档所说,这个包基本上提供了两个 REST 端点:(1)请求令牌和(2)验证(确认)令牌(并更改密码)。 (1) 用户要求新密码时;在我的情况下,我使用电子邮件接收密码重置链接(或令牌),但可能是例如文本消息。
-
感谢您的回复 :) 。我知道这个问题与我要问的无关。我已经阅读了密码重置库的文档。是的,在电子邮件中,我还将发送令牌和链接以更改密码(使用 Angular 组件创建的简单前端页面具有令牌和新密码的输入框),此页面将指向端点验证(确认)令牌(并更改密码)。让我知道你的看法。再次感谢:)
-
@sunil 如果这适用于您的项目,那很好!
标签: python django django-rest-framework django-templates password-recovery