Issue
After clicking on Submit in my form I am getting "Method Not Allowed The method is not allowed for the requested URL."
The required code is below
hello.py
@app.route('/')
def home():
return render_template("home.html")
@app.route('/register', methods=["GET", "POST"])
def register():
if request.method == "POST":
# data = {}
name = request.form['name']
place = request.form['place']
descr = request.form['descr']
email = request.form['email']
collection.insert_one({'name': name, 'place': place, 'email': email, 'descr': descr})
return render_template("register.html")
register.html
<form action="." method="post">
<h1> COMPLAINT FORM </h1>
<input class="box" type="text" name="name" id="name"
placeholder="Name" required /><br>
<input class="box" type="place" name="place" id="place"
placeholder="Place" required /><br>
<input class="box" type="email" name="email" id="email"
placeholder="E-Mail " required /><br>
<input class="box" type="text" name="descr" id="descr"
placeholder="Enter Description " required /><br>
<input type="submit" id="submitDetails"
name="submitDetails" value="Submit" /><br>
</form>
I also tried including a redirect link in form action="." but it throws the same error.
Solution
The problem is in the way you want to redirect from the /register
endpoint to the home. You want to post something to the home so the code should be as follows:
hello.py
@app.route('/', methods=["GET", "POST"])
def home():
return render_template("home.html")
@app.route('/register', methods=["GET", "POST"])
def register():
if request.method == "POST":
# data = {}
name = request.form['name']
place = request.form['place']
descr = request.form['descr']
email = request.form['email']
collection.insert_one({'name': name, 'place': place, 'email': email, 'descr': descr})
return render_template("register.html")
register.html
<form action="/" method="post">
<h1> COMPLAINT FORM </h1>
<input class="box" type="text" name="name" id="name"
placeholder="Name" required /><br>
<input class="box" type="place" name="place" id="place"
placeholder="Place" required /><br>
<input class="box" type="email" name="email" id="email"
placeholder="E-Mail " required /><br>
<input class="box" type="text" name="descr" id="descr"
placeholder="Enter Description " required /><br>
<input type="submit" id="submitDetails"
name="submitDetails" value="Submit" /><br>
</form>
Answered By - Mohamed Mostafa
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.