简体   繁体   中英

POST method in Flask


app = Flask(__name__)

@app.route('/')

def home():
    return render_template("home.html")

@app.route('/login_page', methods=["GET","POST"])

def login_page():
    if request.method == "POST":
        user=request.form["login"]
        return redirect(url_for("user",usr=user))
    else:
        return render_template("login_page.html") 

@app.route('/register_page')

def register_page():
    return render_template("register_page.html")

@app.route('/home', methods=["GET","POST"])

@app.route("/<usr>")

def user(usr):
    return f"<h1>{usr}</h1>"

if __name__ == '__main__':
    app.run(debug=True)

here is my error TypeError: user() missing 1 required positional argument: 'usr', i dont understand where user function is missing argument

here is my folder tree

https://i.stack.imgur.com/5pvkT.png

You've defined your user function like this:

def user(usr)

This says that it requires a single positional argument. You have two routes pointing at this function:

- @app.route('/home', methods=["GET","POST"])
- @app.route("/<usr>")

Only the second route provides the necessary usr variable. When you visit the route /home , you'll get the TypeError you've shown in your question.

Depending on how you want the function to behave when no user is specified, it may make more sense to have a second function handling /home . Maybe that was supposed to be an alternate route for the home function instead?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM