我正在构建一个web应用程序,并尝试使用redirect()函数链接两个html。但是,当我单击“提交”按钮时登录.html没有重定向到成功.html(即浏览器仍在登录.html)在
下面是我的python代码:
from flask import Flask, redirect, url_for, render_template, request, abort
app = Flask(__name__)
@app.route('/')
def index():
return render_template('login.html')
@app.route('/login',methods = ['POST', 'GET'])
def login():
if request.method == 'POST':
if request.form['username'] == 'admin':
return redirect(url_for('success'))
else:
abort(401)
else:
return redirect(url_for('index'))
@app.route('/success', methods = ['POST', 'GET'])
def success():
return 'logged in successfully'
if __name__ == '__main__':
app.run(debug = True)
以及html代码:
^{pr2}$
您的字段名在html和view函数中不匹配。将
name = "nm"
更改为name = "username"
:相关问题 更多 >
编程相关推荐