方法不允许在烧瓶中的错误 [英] method not allowed error in flask

查看:138
本文介绍了方法不允许在烧瓶中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 方法不允许

该方法不允许用于请求的URL。

这里是我的烧瓶代码..

< pre $ @ app.route(/)
def hello():
return render_template(index.html)

@ app.route(/,methods = ['POST','GET'])
def get_form():
query = request.form [search]
打印查询

和我的index.html

 <身体GT; 

< div id =wrap>
< form action =/autocomplete =onmethod =POST>
< input id =searchname =searchtype =textplaceholder =你感觉怎么样?
< input id =search_submitvalue =Sendtype =submit>
< / form>
< / div>

< script src =js / index.js>< / script>

< / body>

编辑我的完整烧录代码:

  from flask import烧瓶,请求,会话,重定向,render_template,url_for 
import flask
print flask .__ version__
app = Flask(__ name__)

@ app.route(/)
def entry():
return render_template(index.html)

@ app.route (/ data,methods = ['POST'])
def entry_post():
query = request.form [search]
打印查询
返回render_template index.html)


if __name__ ==__main__:
app.run()


解决方案

您正在发布到条目() code> entry_post()函数侦听不同的路由;注册只听 / data ,而不是 /

  @ app.route(/ data,methods = ['POST'])
def entry_post():

/ 路径不接受 POST ,默认只有 GET HEAD OPTIONS

相应地调整您的表单:

 < form action =/ dataautocomplete =onmethod =POST>考虑到Flask并不是重新加载你的源代码,除非你      href =http://flask.pocoo.org/docs/quickstart/#debug-mode =nofollow>启用调试功能 

 app.run(debug = True)


I am getting this error when I try to submit a request.

Method Not Allowed

The method is not allowed for the requested URL.

And here is my flask code..

@app.route("/")
def hello():
  return render_template("index.html")

@app.route("/", methods=['POST','GET'])
def get_form():
  query = request.form["search"]
  print query

And my index.html

<body>

<div id="wrap">
  <form action="/" autocomplete="on" method="POST">
    <input id="search" name="search" type="text" placeholder="How are you feeling?">
     <input id="search_submit" value="Send" type="submit">
  </form>
</div>

  <script src="js/index.js"></script>

</body>

Edit.. My complete flask code:

from flask import  Flask,request,session,redirect,render_template,url_for
import flask
print flask.__version__
app = Flask(__name__)

@app.route("/")
def entry():
    return render_template("index.html")

@app.route("/data", methods=['POST'])
def entry_post():
    query = request.form["search"]
    print query
    return render_template("index.html")


if __name__ == "__main__":
    app.run()

解决方案

You are posting to the entry() function, while your entry_post() function listens to a different route; it is registered to only listen to /data, not /:

@app.route("/data", methods=['POST'])
def entry_post():

The / route doesn't accept POST, by default only GET, HEAD and OPTIONS are allowed.

Adjust your form accordingly:

<form action="/data" autocomplete="on" method="POST">

Take into account that Flask does not reload your source unless you enable debugging:

app.run(debug=True)

这篇关于方法不允许在烧瓶中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆