简体   繁体   English

Python Flask App 路由未按预期工作

[英]Python Flask App route not working as expected

I am not able to get the 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 it gives old output which I have tried (some other string "Hello flask") instead of "hello world" I have tried closing my IDE and trying again even I have created another project but the problem still persist每次给出 404 时,我都无法从浏览器获取http://127.0.0.1:5000/user1 Url,而对于http://127.0.0.1:5000,它给出了我尝试过的旧输出(其他一些字符串 "Hello flask") 而不是 "hello world" 我已经尝试关闭我的 IDE 并再次尝试,即使我已经创建了另一个项目,但问题仍然存在

Thanks in advance!提前致谢!

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

Are you restarting the server when reloading @app.route('/') ?重新加载@app.route('/')时是否重新启动服务器? Also, try clearing your cache.另外,尝试清除缓存。 With Chrome, you can do that by hitting SHIFT + F5.使用 Chrome,您可以通过按 SHIFT + F5 来实现。

@app.route('/user1', methods=['POST']) moves to abort since you are not posting anything in your view. @app.route('/user1', methods=['POST'])移动到中止,因为您没有在视图中发布任何内容。 What exactly are you trying to do in this route?你到底想在这条路线上做什么?

Also, while developing, I recommend running app run with the debug parameter:此外,在开发时,我建议使用 debug 参数运行 app run:

app.run(debug=True)

尝试在app.run()指定端口

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

I too faced the same issue.我也面临同样的问题。 Sometimes there might installation issues I guess.有时我猜可能会有安装问题。

In my case I just pip uninstalled flask and reinstalled and it worked fine ;)在我的情况下,我只是 pip 卸载了烧瓶并重新安装,它工作正常;)

To pip uninstall use :要 pip 卸载使用:

pip uninstall flask

and check whether the servers aren't working, it obviously shouldn't then reinstall by并检查服务器是否不工作,显然不应该重新安装

pip install flask

;) ;)

Are you submitting a form with the parameter (email_id) you set?您是否使用您设置的参数 (email_id) 提交表单? Also, make sure you are submitting the form with Content-Type: application/json - otherwise, request.json will be empty.另外,请确保您使用 Content-Type: application/json 提交表单 - 否则 request.json 将为空。 You can also use request.get_json(force=True) , but this is not recommended.您也可以使用request.get_json(force=True) ,但不推荐这样做。 (See How to get POSTed json in Flask? ) (请参阅如何在 Flask 中获取 POSTed json?

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

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