Skip to content

Why does my Flask route return a 405 error when I submit a form? #5773

Discussion options

You must be logged in to vote

Great question!

The issue is that your route is only set to accept GET requests by default, since you didn’t specify the allowed methods.

To fix the 405 error, you need to explicitly allow POST in the route definition using the methods parameter:

@app.route("/submit", methods=["GET", "POST"])
def submit():
    if request.method == "POST":
        name = request.form["name"]
        return f"Hello, {name}!"
    return "Invalid method"
This tells Flask to accept both GET and POST requests on that route.

Let me know if it works! ✅

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@MatteoMgr2008
Comment options

Answer selected by MatteoMgr2008
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants