仅按用户名重置密码 [英] Password Reset only by Username

查看:75
本文介绍了仅按用户名重置密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I我隐含使用电子邮件重设django密码,但当前只想限制为用户名,以便用户只能使用用户名来更改密码。已尝试内置django 插件,但无法设置为仅用户名。任何建议将不胜感激

I Implimented django password reset using email but currently want to restrict to only username so that user can change password only by using username. tried django built-in and plugin but unable to set to only username . Any suggestions would be appreciated

推荐答案

编写您自己的重置密码。几乎唯一需要编写的代码就是获取用户,生成用于重置密码的唯一链接并发送电子邮件的第一步。

Write your own reset password code. Pretty much the only code you need to write is the first step that gets the user, generates one-only link for resetting password and sends email.

Django使用PasswordResetForm做到了这一点。 。源代码是此处(第242行- 306)。将此代码复制/粘贴到您的 forms.py (包括必需的导入文件)中,并对其进行修改以接受用户名而不是电子邮件。

Django does that with PasswordResetForm. Source code is here (lines 242 - 306). Copy/paste this code to your forms.py (including required imports) and modify it to accept username instead of email.

然后在您的代码中验证您的PasswordResetForm并调用 save()以生成一个唯一的链接并发送邮件。

In your code you then validate your PasswordResetForm and call save() to generate one-only link and send mail.

form = YourPasswordResetForm(request.POST)
if form.is_valid():
    form.save()

记住始终返回成功消息,即使用户不存在且电子邮件不存在发送,因为否则您将给出有关注册用户名的信息。

Remember to always return success message, even if user doesn't exist and email wasn't sent, because otherwise you'll be giving out information about registered usernames.

这篇关于仅按用户名重置密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆