I have the page app route. This is my python flask code.
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)
When I run it it redirects to http://localhost:5000 when it should redirect to http://localhost:5000/dashboard .
Whats happening?
Add an import for url_for
:
from flask import url_for
Then use it in the redirect:
return redirect(url_for("dashboard"))
The url_for
function takes the name of a function and constructs the "url for" that function.
You posted a link to your project files on DropBox.
In your template, registerPage.html
, you need to change:
<form method="POST" action="/">
to:
<form method="POST" action="/register">
If you post to /
instead, the register
function will never see the posted data and will never redirect the user to the dashboard.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.