Home > Blockchain >  "Method Not Allowed" error in Flask after submitting form data
"Method Not Allowed" error in Flask after submitting form data

Time:02-08

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  type="text" name="name" id="name" 
            placeholder="Name"  required /><br>

            <input  type="place" name="place" id="place" 
            placeholder="Place" required /><br>
                              
            <input  type="email" name="email" id="email" 
            placeholder="E-Mail " required /><br>

            <input  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.

CodePudding user response:

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  type="text" name="name" id="name" 
            placeholder="Name"  required /><br>

            <input  type="place" name="place" id="place" 
            placeholder="Place" required /><br>
                              
            <input  type="email" name="email" id="email" 
            placeholder="E-Mail " required /><br>

            <input  type="text" name="descr" id="descr" 
            placeholder="Enter Description " required /><br>
                          
            <input type="submit" id="submitDetails"  
            name="submitDetails" value="Submit" /><br>
                      
            </form>
  •  Tags:  
  • Related