python - 烧瓶重定向功能未重定向到正确的页面
问题描述
我有页面应用程序路线。这是我的 python 烧瓶代码。
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”。
您在 DropBox 上发布了指向您的项目文件的链接。
在您的模板中registerPage.html
,您需要更改:
<form method="POST" action="/">
至:
<form method="POST" action="/register">
如果您/
改为发布,该register
函数将永远不会看到发布的数据,也永远不会将用户重定向到仪表板。
推荐阅读
- javascript - 如何使用 JavaScript 打开 Google Assistant App?
- java - LinearLayoutManager 不能在 Fragment 中使用
- asp.net - NLog获取当前用户会导致出现错误
- latex - 使用 RMarkdown 编织“大于或等于”符号时 PDF 输出失败
- flash - 闪存实现中的循环缓冲区
- java - 无法在 JSP 页面中显示图像
- javascript - 使用回调函数查询
- c++ - 为什么在 c++20 中不推荐使用 std::filesystem::u8path?
- android - 如何将数据从 Activity 传递到 CustomView?
- javascript - 如何通过 sequelize.js 使用云端数据库?