我正在考虑为Django项目实现用户身份验证。我正在阅读documentation。它似乎很简单,但有一件事我不明白。
显然,身份验证包括八个视图:
accounts/login/ [name='login']
accounts/logout/ [name='logout']
accounts/password_change/ [name='password_change']
accounts/password_change/done/ [name='password_change_done']
accounts/password_reset/ [name='password_reset']
accounts/password_reset/done/ [name='password_reset_done']
accounts/reset/<uidb64>/<token>/ [name='password_reset_confirm']
accounts/reset/done/ [name='password_reset_complete']
实现密码重置时,我假设我想要做的是实现accounts/password_reset/
,它会向用户转发电子邮件。然后,我需要实现accounts/reset/<uidb64>/<token>/
,这是用户通过电子邮件定向的地方。我不清楚的是当用户成功更新密码时应该做什么。
accounts/reset/done/
(或password_resest_complete
)和accounts/password_reset/done/
(或password_reset_done
)有什么区别?
好问题。这是他们的样子:
class PasswordResetCompleteView(PasswordContextMixin, TemplateView):
template_name = 'registration/password_reset_complete.html'
title = _('Password reset complete')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['login_url'] = resolve_url(settings.LOGIN_URL)
return context
class PasswordResetDoneView(PasswordContextMixin, TemplateView):
template_name = 'registration/password_reset_done.html'
title = _('Password reset sent')
主要区别在于PasswordResetCompleteView
将LOGIN_URL
传递给您的模板上下文。
password_reset_done
在发送电子邮件时显示成功消息(在password_reset
中输入电子邮件后)。当密码成功更改时,password_reset_complete
会显示成功消息。
如果你看一下这些观点的source code,就会有一个解释这个过程的评论。第237-242行:
# 4 views for password reset:
# - password_reset sends the mail
# - password_reset_done shows a success message for the above
# - password_reset_confirm checks the link the user clicked and
# prompts for a new password
# - password_reset_complete shows a success message for the above