Why does my Flask route return a 405 error when I submit a form? #5773
Answered
by
Istituto-freudinttheprodev
MatteoMgr2008
asked this question in
Q&A
|
I'm using Flask and trying to handle a form submission with POST, but I keep getting a 405 Method Not Allowed error. Here's the basic code: from flask import Flask, request
app = Flask(__name__)
@app.route("/submit")
def submit():
if request.method == "POST":
name = request.form["name"]
return f"Hello, {name}!"
return "Invalid method"I submit the form from an HTML page using method="POST", but Flask still returns a 405. What am I doing wrong? |
Answered by
Istituto-freudinttheprodev
Jul 14, 2025
Replies: 2 comments 1 reply
|
Because you need |
0 replies
|
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 @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! ✅ |
1 reply
Answer selected by
MatteoMgr2008
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
POSTin the route definition using themethodsparameter:Let me know if it works! ✅