简体   繁体   English

Python flask flask不允许的方法所请求的URL不允许使用该方法

[英]Python flask Method Not Allowed The method is not allowed for the requested URL

I am using flask to deploy my deep learning model. 我正在使用flask来部署我的深度学习模型。

But when I am running my model I got this following error. 但是当我运行模型时,出现以下错误。

Method Not Allowed The method is not allowed for the requested URL 不允许的方法所请求的URL不允许使用该方法

And when I activate debug mode I got this 当我激活调试模式时,我得到了

flask.cli.NoAppException flask.cli.NoAppException: While importing "app", an ImportError was raised: flask.cli.NoAppException flask.cli.NoAppException:导入“ app”时,引发了ImportError:

Traceback (most recent call last): File "c:\\users\\tab\\anaconda3\\lib\\site-packages\\flask\\cli.py", line 235, in locate_app import (module_name) File "C:\\Chatbot-Flask-Server-master\\Chatbot-Flask-Server-master\\app.py", line 3, in import tensorflow as tf ModuleNotFoundError: No module named 'tensorflow' 追溯(最近一次通话最近):在locate_app 导入 (模块名称)的第235行中,文件“ c:\\ users \\ tab \\ anaconda3 \\ lib \\ site-packages \\ flask \\ cli.py”在文件“ C:\\ Chatbot-Flask- Server-master \\ Chatbot-Flask-Server-master \\ app.py“,第3行,在导入tensorflow中作为tf ModuleNotFoundError:没有名为'tensorflow'的模块

I had installed my tensorflow and the model can running well without flask, an error still appears when I activate virtual environment. 我已经安装了我的tensorflow,并且该模型可以在没有烧瓶的情况下正常运行,激活虚拟环境时仍然会出现错误。

And here my code : 这是我的代码:

## webapp
app = Flask(__name__, template_folder='./')


@app.route('/prediction', methods=['POST', 'GET'])
def prediction():
    response =  pred(str(request.json['message']))
    return jsonify(response)

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


if __name__ == '__main__':
   app.run(debug=True)

And the index 和索引

 <form action="http://127.0.0.1:5000/" method='POST' , 'GET'> <p>Text <input type="text" name="message" style="height:200px" /></p> <p><input type="submit" value="submit" /></p> </form> 

Virtual Environment 虚拟环境

You mention in the post you had installed Tensorflow and it works without the virtual environment, so the error possibly comes from the fact that you haven't installed Tensorflow in the Virtual Environment itself. 您在帖子中提到您已经安装了Tensorflow,并且在没有虚拟环境的情况下也可以工作,因此错误可能是由于您尚未在虚拟环境本身中安装Tensorflow而引起的。 This is because when you create a Virtual Environment using Python, for such: 这是因为在使用Python创建虚拟环境时 ,例如:

python -m venv venv

In the virtual environment, there is only the most recent of Python that you have available but not other packages you have in outside of Virtual Environment. 在虚拟环境中,只有最新的Python可用,而在虚拟环境之外没有其他软件包。

Solution: Activate the virtual environment, then install Tensorflow INSIDE of the virtual environment. 解决方案:激活虚拟环境,然后安装虚拟环境的Tensorflow INSIDE。

Flask Request 烧瓶要求

It is obvious that when you submit the form, it is a POST request, but in your index view, you only handle GET request. 显然,当您提交表单时,它是一个POST请求,但是在index视图中,您仅处理GET请求。 If you don't know what POST and GET request it, essentially: 如果您不知道是什么POSTGET请求,本质上是:

  • POST is used to send data to the server. POST用于将数据发送到服务器。
  • GET is used to request data from the server. GET用于从服务器请求数据。

In your case, you want to send the form data to the server, so it is a POST request. 对于您的情况,您想将表单数据发送到服务器,因此它是一个POST请求。 All you need to do is to modify your view function so that it could handle POST as well. 您需要做的就是修改视图函数,以便它也可以处理POST

from flask import request

@app.route("/", methods=["POST", "GET"])
def main():
    // when the request is post request
    if request.method == "POST":
        // get data from the form
        data = request.form["message"]

        // do something here

    return render_template('index.html')

 <form action="http://127.0.0.1:5000/" method='POST'> <p>Text <input type="text" name="message" style="height:200px" /></p> <p><input type="submit" value="submit" /></p> </form> 

Jinja Template Jinja模板

Jinja is the template engine for Python. Jinja是Python的模板引擎。 Basically, with the help of Jinja, you could pass variables from the server side to the client side and do all the logic in the client sides. 基本上,在Jinja的帮助下,您可以将变量从服务器端传递到客户端,并在客户端执行所有逻辑。 In your case, you want to pass the data you got from the front-end back to the front-end, you could do: 对于您的情况,您要将从前端获得的数据传递回前端,可以执行以下操作:

from flask import request

@app.route("/", methods=["POST", "GET"])
def main():
    // when the request is post request
    if request.method == "POST":
        // get data from the form
        data = request.form["message"]

        // pass the data back to the front end
        return render_template("index.html", message=data)

    // message is none here because this is a get request
    return render_template('index.html', message=None)

and your index.html : 和您的index.html

 <form action="http://127.0.0.1:5000/" method='POST' , 'GET'> <p>Text <input type="text" name="message" style="height:200px" /></p> <p><input type="submit" value="submit" /></p> </form> {% if message %} <h1>{{ message }}</h1> {% endif %} 

What happens in the front end? 前端会发生什么? Well, it checks to see if the message variable from the server is None or not. 好吧,它检查服务器的message变量是否为None If it is not None , then render the message variable, otherwise, do nothing. 如果不是None ,则渲染message变量,否则不做任何事情。

You could learn more about Jinja here :) 您可以在这里了解有关Jinja的更多信息:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python、Flask:方法不允许,请求的方法不允许 URL - Python, Flask: Method Not Allowed, The method is not allowed for the requested URL Python Flask:错误“请求的 URL 不允许使用该方法” - Python Flask: Error "The method is not allowed for the requested URL" 不允许的方法 请求的 URL 不允许使用该方法。 在 DELETE 方法烧瓶上 - Method Not Allowed The method is not allowed for the requested URL. on DELETE Method Flask Flask 错误:“方法不允许 请求的 URL 不允许该方法” - Flask Error: “Method Not Allowed The method is not allowed for the requested URL” Flask 错误:“方法不允许 请求的 URL 不允许该方法” - Flask Error: "Method Not Allowed The method is not allowed for the requested URL" Flask 错误:方法不允许 请求的 URL 不允许该方法 - Flask error: Method Not Allowed The method is not allowed for the requested URL Flask 错误:方法不允许 请求的 URL 不允许该方法 - Flask error : Method Not Allowed The method is not allowed for the requested URL Flask - POST - 请求的URL不允许使用该方法 - Flask - POST - The method is not allowed for the requested URL 所请求的URL不允许使用该方法。 在烧瓶 - The method is not allowed for the requested URL. in Flask 如何修复 Flask 中的“请求的 URL 不允许使用该方法” - How to fix "The method is not allowed for the requested URL" in Flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM