Issue
This Content is from Stack Overflow. Question asked by avinator
I am trying to create a login page that sends the username to the method it redirects to.
@app.route ("/login/", methods=["POST","GET"])
def login ():
if request.method=="POST":
name = request.form.get ("fname")
user = Users.query.filter_by(first_name=name).first ()
name=name.lower ()
print (name)
return redirect(url_for("fav_club", username=name))
return render_template("login.html")
@app.route ("/favclub/<username>", methods=["POST","GET"])
def fav_club (username):
if request.method == "POST":
club = Club.query.filter_by(code=request.form.get ("fccode")).first ()
club.fav_counter += 1
user = Users.query.filter_by(first_name=request.form.get(username)).first()
fav = Favorites(favorite=club, users=user)
db.session.commit()
return "Added Favorite!"
else:
return render_template("favorites.html")
I get the following error: werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'fav_club'. Did you forget to specify values ['username']?
Every time I try to use dynamic routng, I get an error like this. I do not think this is an HTML issue.
Solution
In the fav_club view, you specify a "username" parameter as part of the URL. However, the URL generated by url_for('fav_club')
in favorites.html doesn’t include a username, so when you try to render the template Flask tells you that the username is missing.
It is probably better to remove the "username" field from the URL entirely. That way you don’t have to supply it when rendering the page from a GET request, and you can supply it from the form data when submitting the form as a POST request. (Indeed, you have already added the code for handling the form data to the fav_code view.)
@app.route("/login", methods=["POST","GET"])
def login():
if request.method=="POST":
name = request.form.get("fname")
user = Users.query.filter_by(first_name=name).first ()
name=name.lower()
return redirect(url_for("fav_club"))
return render_template("login.html")
@app.route("/favclub", methods=["POST","GET"])
def fav_club():
if request.method == "POST":
club = Club.query.filter_by(code=request.form.get ("fccode")).first ()
club.fav_counter += 1
user = Users.query.filter_by(first_name=request.form.get(username)).first()
fav = Favorites(favorite=club, users=user)
db.session.commit()
return "Added Favorite!"
else:
return render_template("favorites.html")
favorites.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Find a Club!</title>
</head>
<body>
<form action="{{ url_for('fav_club') }}" method="post">
<label for="club">Favorite Club Code:</label>
<input type="text" id="club" name="fccode" placeholder="favclubcode">
<input type="test" id="username" name="username" placeholder="Username">
<button type="submit">Submit</button>
</form>
</body>
</html>
This will solve your error, but the login page has no effect – you can just skip straight to the /favclub page, and provide any username you like. To make logging in work correctly, you need to use sessions. See, e.g., this tutorial on how to do that.
This Question was asked in StackOverflow by avinator and Answered by Jack Taylor It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.