Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

python - Why is my HTTP Post not working in flask?

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...