【问题标题】:Flask - 405, Method not allowed烧瓶 - 405,方法不允许
【发布时间】:2016-06-18 07:04:58
【问题描述】:

我正在尝试使用 POST 方法将这个简单的表单数据发送到 MySQL 数据库。但同样的错误“方法不允许”,我不知道那有什么问题。请帮帮我。

HTML 代码:

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

烧瓶:

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))

用户资料路线:

@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)) 

【问题讨论】:

    标签: python mysql python-2.7 flask flask-admin


    【解决方案1】:

    检查/userprofile 接受的方法。可能这个视图函数设置为只接受发布请求?

    来自updateaprof() 的重定向将导致浏览器向新位置/userprofile 发出GET 请求,如果它不接受GET 请求,将返回您所看到的405 Method Not Allowed。

    如果这是问题,您可以通过将GET 添加到支持的方法并在您的视图中处理 GET 请求来解决它。

    / 也可能有类似的问题,尽管不支持“索引”URL 上的 GET 请求是不常见的。

    【讨论】:

    • 查看上面的方法userprofile(),有什么问题吗?
    • @LOKESHTIWARI:嗯,可能不是。你也检查/吗?此外,updateaprof() 中的 SELECT 查询看起来是错误的 - 您应该将一个元组传递给 execute()。这段代码应该会导致异常,所以重定向到/userprofile 可能不会发生,也许问题真的出在/ 上?最后你不需要关闭游标和查询之间的连接,也不需要在SELECT之后提交。
    • 重新启动服务器后,我再次编写了代码......现在它可以工作了......我还删除了游标、连接并提交 b/w 查询。谢谢你帮助我:)
    猜你喜欢
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 2014-01-22
    • 2014-07-28
    • 2020-07-30
    • 1970-01-01
    相关资源
    最近更新 更多