python - 如果用户在登录时忘记了密码,则更新密码,Python Flask
问题描述
如果用户在登录时忘记了密码,我正在尝试更新密码。但它不断地给出下面给出的相同错误。
错误请求:浏览器(或代理)发送了此服务器无法理解的请求。
应用程序.py
@app.route('/NewPassword', methods=['POST', 'GET'])
def NewPassword():
if request.method == 'POST':
OldEmail = request.form['NewPswdInputEmail']
OldPhone = request.form['NewPswdInputPhoneNo']
NewPaswd = request.form['NewPaswdInputPassword']
cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cur.execute("UPDATE USERS SET Paswd='" + NewPaswd + "' WHERE Email ='" + OldEmail + "' OR Phone ='" + OldPhone + "'")
cur.connection.commit()
return "Password Updated"
return render_template('NewPassword.html')
新密码.html
<form action="/NewPassword" method="post" oninput='NewPaswdConPassword.setCustomValidity(NewPaswdConPassword.value != NewPaswdInputPassword.value ? "Password does not match." : "")'>
<div class="form-group">
<label for="customRadio">Select Option to Change Password Between Email & Phone No.</label>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="customRadioInline1" name="customRadioInline" value="Email" class="custom-control-input" checked>
<label class="custom-control-label" for="customRadioInline1">Email Id</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="customRadioInline2" name="customRadioInline" value="Phone" class="custom-control-input">
<label class="custom-control-label" for="customRadioInline2">Phone No.</label>
</div>
</div>
<div class="form-group" id="EmailShowHide">
<label for="NewPswdInputEmail">Email Address</label>
<input type="email" class="form-control" name="loginInputEmail" id="NewPswdInputEmail" aria-describedby="emailHelp" placeholder="Email address">
<small id="loginemailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group" id="PhoneShowHide" style="display: none">
<label for="NewPswdInputPhoneNo">Phone Number</label>
<input type="tel" id="NewPswdInputPhoneNo" name="NewPswdInputPhoneNo" class="form-control" pattern="[0-9]{4}[0-9]{2}[0-9]{4}" placeholder="Phone number">
<small id="NewPswdPhoneHelpInline" class="text-muted">Enter Valid Number</small>
</div>
<div class="form-group">
<div class="tool">
<label for="NewPaswdInputPassword">New Password</label>
<div class="input-group" id="NewPaswdShowHide">
<input type="password" class="form-control" name="NewPaswdInputPassword" id="NewPaswdInputPassword" pattern="^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{8,20}$" placeholder="New password" required>
<div class="input-group-append">
<a class="btn btn-primary">
<i class="bi bi-eye-slash" aria-hidden="true"></i>
</a>
</div>
</div>
<span class="tooltext">Password Must be Alphanumeric with atleast an UpperCase & LowerCase Characters</span>
<small id="NewPaswdHelpInline" class="text-muted">Must be 8-20 characters long.</small>
</div>
</div>
<div class="form-group">
<label for="NewPaswdInputConPassword">Confirm New Password</label>
<input type="password" class="form-control" name="NewPaswdConPassword" id="NewPaswdInputConPassword" placeholder="Confirm new password" required>
</div>
<button type="submit" name="Login" id="NewPaswdSubmit" class="btn btn-primary">Submit</button>
</form>
我想做的是,如果我的数据库“用户”有电子邮件或电话号码。然后 Paswd 将使用用户输入的新密码进行更新
请告诉我我做错了什么。
解决方案
我假设您正在使用 Flask。您的应用程序是否在debug
模式下运行?在调试模式下运行应用程序有助于确定问题所在。
问题在于电子邮件 HTML 表单输入。当您提交表单时,数据会key=value
成对发送,在这种情况下,您使用的是 Flask,因此它是一个 python 字典。因此,在/NewPassword
路由中,当您请求时request.form['NewPswdInputEmail']
,您将获取从表单提交的字典中的一个值,并将其'NewPswdInputEmail'
作为键。在表单输入中,键是name
属性。
这就是为什么你得到的原因,KeyError
因为name
电子邮件输入表单中name="loginInputEmail"
的name="NewPswdInputEmail"
您需要做的就是将loginInputEmail
电子邮件输入字段中的属性重命名为NewPswdInputEmail
.
推荐阅读
- python - 如何使用 Python 与 Websocket 通信
- c# - 在具有默认约束的分区视图中插入数据时出现问题
- continuous-integration - Teamcity 突然无法与 VCS 根通信
- reactjs - 无法使用 next-redux-wrapper 让 Redux (Thunks) 与 NextJS 一起工作:未调用 getInitalProps
- packer - Azure 上的 Packer 与 VM:等待 WinRM 的超时
- r - 识别 data.table 中列表(作为变量)中的相同列表
- java - 错误:appengine-maven-plugin:1.3.2:运行失败:非零退出:1
- string - 在 powershell 中使用搜索字符串编辑文件
- javascript - 在对象文字中声明时,包含“this”的数组元素返回未定义
- sql - 使用 VBA 上传数百条记录