gpt4 book ai didi

python - flask 中的方法不允许错误

转载 作者:太空狗 更新时间:2023-10-29 16:06:52 25 4
gpt4 key购买 nike

我在尝试提交请求时遇到此错误。

Method Not Allowed

The method is not allowed for the requested URL.

这是我的 flask 代码..

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

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

还有我的 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>

编辑..我完整的 flask 代码:

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

最佳答案

您正在向 entry() 函数发帖,而您的 entry_post() 函数监听一个不同的路由;它被注册为只听/data,而不是/:

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

/ 路由不接受POST,默认只接受GETHEAD允许选项

相应地调整您的表单:

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

请注意 Flask 不会重新加载您的源代码,除非您 enable debugging :

app.run(debug=True)

关于python - flask 中的方法不允许错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24088054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com