简体   繁体   English

即使定义了路线,发布到Flask app也会给出404

[英]Posting to Flask app gives 404 even though route is defined

I ran the following Flask app and tried to post to / , but I got a 404 response. 我运行了以下Flask应用并尝试发布到/ ,但我收到了404响应。 The / route is defined, why doesn't it get handled? / route是定义的,为什么不处理它?

from flask import Flask, request

app = Flask(__name__)
app.run()

@app.route('/', methods=['POST'])
def handle_post_request():
    return str(request.form)
C:\Python35\python.exe D:/Dropbox/dev/contact.py
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [03/Apr/2017 20:46:01] "POST / HTTP/1.1" 404 -

Calling app.run blocks, no code is executed after it until the server is stopped. 调用app.run块,之后不会执行任何代码,直到服务器停止。 Make sure your app is completely set up before calling run. 在调用run之前确保您的应用程序已完全设置。 In this case, move the route definition above the call to run . 在这种情况下,将路径定义移到要run的调用上方。


Flask 0.11 introduced a more robust way to start applications to avoid this and other common mistakes. Flask 0.11引入了一种更强大的启动应用程序的方法,以避免这种和其他常见错误。 Completely remove app.run() . 完全删除app.run() Set the FLASK_APP environment variable to your app's entry point, and call flask run . FLASK_APP环境变量设置为应用程序的入口点,然后调用flask run

FLASK_APP=contact.py flask run

You can also set FLASK_DEBUG=1 to enable debug mode. 您还可以设置FLASK_DEBUG=1以启用调试模式。

In Windows, use set FLASK_APP=contact.py to set environment variables instead. 在Windows中,使用set FLASK_APP=contact.py来设置环境变量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM