Search code examples
pythonmysqlpython-2.7flaskflask-admin

Flask - 405, Method not allowed


I'm trying to send this simple form data to MySQL database using POST method. but same error "Method not allowed", I don't know what's wrong with that. Please help me out here.

HTML Codes:

<form role="form" action='/updateabbprof' method="POST">

          <div class="form-group">
            <label class="control-label">About Us</label>
            <textarea class="form-control"  name="updabt" id="editabt"></textarea>
          </div>

      <div class="form-group">
        <button id="aupdate" type="submit" class="btn btn-primary">Save</button>
      </div>
      </form>
</div>

Flask:

app.route('/updateabbprof', methods = ['POST'])
def updateaprof():
        try:

                if session.get('user'):
                    _userid = session.get('user')
                    _userabt = str(request.form['updabt'])


                    #if _userid or _username or _email or _phone:
                    con = mysql.connect()
                    cursor = con.cursor()
                    cursor.execute("""UPDATE user_signpp SET user_abt = %s WHERE Id =%s""",(_userabt,_userid))
                    con.commit()
                    cursor.close()
                    con.close()
                    con = mysql.connect()
                    cursor = con.cursor()
                    cursor.execute("""SELECT * FROM user_signpp WHERE Id = %s""",(_userid))
                    con.commit()
                    datanew = cursor.fetchall()
                    session['msg'] = list(datanew[0])
                    cursor.close()
                    con.close()
                    return redirect("/userprofile")
                else:
                    return redirect('/')
        except Exception as e:
                return render_template('error.html', error = str(e))

Userprofile route:

@app.route('/userprofile', methods = ['GET','POST'])
def userprofile():
        try:
            if session.get('user'):

                        return render_template('profile.html', msg = session.get('msg'))
            else:
                        return redirect('/')
        except Exception as e:
            return render_template('error.html', error = str(e)) 

Solution

  • Check the methods that are accepted by /userprofile. Probably this view function is set up to accept post requests only?

    The redirect from updateaprof() will cause the browser to issue a GET request to the new location /userprofile, which if it doesn't accept GET requests, will return the 405 Method Not Allowed that you are seeing.

    If this is the problem you can solve it by adding GET to the supported methods and handling the GET request in your view.

    It's also possible that / has a similar problem, although it's unusual to not support GET requests on the "index" URL.