Reputation: 89
I have my Flask application, with a profile
function down below:
@app.route('/profile/')
def profile(username, password):
return f"<h1>username entered: {username} password entered: {password}</h1>"
I also have my login
function, that uses login.html
as a template, and redirects to profile
with the username
and password
.
Here is my login function:
@app.route('/login/', methods=['POST', 'GET'])
def login():
if request.method == "GET":
return render_template('login.html')
elif request.method == "POST":
username = request.form['username']
password = request.form['password']
print(f'Username: {username}, Password: {password}')
return redirect(url_for('profile', username=username, password=password))
Here is my index.html
file (only the body part)
<h1>Login</h1>
<form action="#" method="POST">
<label>Username</label>
<input type="text" name="username">
<label>Password</label>
<input type="password" name="password">
<button>Submit</button>
</form>
However, when I try to login and then submit, I get a TypeError, saying that my profile
function is missing the two parameters. I got my username and password to print on the terminal, so I know that my login function is working well. Why does it have this error, and how do I fix it?
Upvotes: 0
Views: 200
Reputation: 88
The parameters in your profile function should correspond to url parameters. You will need to update the URL like so:
@app.route('/profile/<string:username>/<string:password>')
def profile(username, password):
return f"<h1>username: {username} password entered: {password}</h1>
A more practical use of url parameters would be for example: fetching a user's profile given a user id
@app.route('/profile/<int:userId>', methods=["GET"])
def profile(userId):
user = getUserById(userId)
return f"<h1>username: {user["username"]}. userId: {user["id"]}</h1>
Theoretically the request would look like the following: GET /profile/20
and the response: <h1>username: foo. userId: 20</h1>
Upvotes: 1