【发布时间】:2025-11-28 08:40:02
【问题描述】:
我有一个登录页面为login.py,它呈现login.html。在提交时我想重定向到mainpage.py,它呈现mainpage.html。但是在提交点击它只是不重定向。我收到一个找不到页面的错误。并且网址保持不变
@app.route('/login',methods = ['GET','POST'])
def index():
try:
if request.method =='POST':
db_user = request.form['db_user']
db_pwd = request.form['db_pwd']
if (connect(db_user,db_pwd)==1):
return redirect("/mainpage", code=302)
else:
return render_template('login.html')
except Exception as e:
print(("error :", str(e)))
return render_template('login.html')
重定向选项中应该提到什么?是呈现 html 的 html 文件名还是 py 文件名?我尝试使用 html 文件名、py 文件名和 @app.route 中的名称。但没有成功
【问题讨论】:
-
尝试为 db connect 添加
else块。 -
那么重定向方法正确吗?这应该是这里提到的 html 文件名还是 .py 文件名?
-
@kten
redirect(url_for("your_function_name"))更好,但指定一个 URL,如/mainpage应该没问题。 -
应该在其他地方定义这个链接(/main)吗?除了 mainpage.py 本身?