[英]Flask redirect function not redirecting to the correct page
我有页面应用路线。 这是我的python flask代码。
from flask import Flask, render_template, flash, redirect
from forms import RegistrationForm, LoginForm
app = Flask(__name__)
@app.route("/")
@app.route("/home", methods=["GET", "POST"])
def home():
return render_template("home.html")
@app.route("/dashboard", methods=["GET", "POST"])
def dashboard():
return render_template("dashboard.html")
@app.route("/register", methods=["GET", "POST"])
def register():
form = RegistrationForm()
app.logger.debug(form.validate_on_submit())
if form.validate_on_submit():
return redirect("dashboard")
return render_template("registerPage.html", title="Register", forms=form)
当我运行它时,它将重定向到http:// localhost:5000 ,而应将其重定向到http:// localhost:5000 / dashboard 。
发生了什么?
为url_for
添加导入:
from flask import url_for
然后在重定向中使用它:
return redirect(url_for("dashboard"))
url_for
函数采用一个函数的名称并构造该函数的“ url for”。
您在DropBox上发布了指向项目文件的链接。
在模板中, registerPage.html
,您需要更改:
<form method="POST" action="/">
至:
<form method="POST" action="/register">
如果您发布到/
,则register
功能将永远不会看到发布的数据,也永远不会将用户重定向到仪表板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.