Python Flask App 路由未按预期工作

Python Flask App route not working as expected

我无法获得 http://127.0.0.1:5000/user1 Url from the browser every time it gives 404 and for the http://127.0.0.1:5000 它给出了我尝试过的旧输出(一些其他字符串 "Hello flask")而不是 "hello world" 我已经尝试关闭我的 IDE 并再次尝试,即使我已经创建了另一个项目,但问题仍然存在

提前致谢!

#imports
app = Flask(__name__) 
@app.route('/')
def hello_world():
    return 'hello world!'

#add users to collection "app_users"
@app.route('/user1',methods=['POST'])
def add_user():
    if not request.json or not 'email_id' in request.json:
       abort(404)
    print("processing")
    insert(request)
    return flask.jsonify({'status': "inserted successfully"}), 201

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

重新加载时是否重新启动服务器@app.route('/')?另外,尝试清除缓存。使用 Chrome,您可以通过按 SHIFT + F5 来完成此操作。

@app.route('/user1', methods=['POST']) 移动到中止,因为您没有在您的视图中发布任何内容。你到底想在这条路线上做什么?

此外,在开发过程中,我建议使用 运行ning 应用程序 运行 和调试参数:

app.run(debug=True)

尝试在 app.run()

中指定端口

app.run(debug=True, host='0.0.0.0', port=9000, threaded=True)

您提交的表单是否带有您设置的参数 (email_id)?此外,请确保您提交的表单内容类型为:application/json - 否则,request.json 将为空。您也可以使用 request.get_json(force=True),但不推荐这样做。 (参见 How to get POSTed json in Flask?

我也遇到了同样的问题。我猜有时可能会出现安装问题。

就我而言,我只是 pip 卸载了 flask 并重新安装,它运行良好;)

要 pip 卸载使用:

pip uninstall flask

并检查服务器是否不工作,显然不应该然后通过

重新安装
pip install flask

;)