Python Flask 应用程序无一例外地引发 500 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18059937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flask app raises a 500 error with no exception
提问by Slater Victoroff
I've been banging my head against this method in Flask for some time, and while it seems I'm making progress now, I've just happened upon something that baffles me to no end. Here is the method I'm calling:
一段时间以来,我一直在反对 Flask 中的这种方法,虽然我现在似乎正在取得进展,但我刚刚发生了一些让我感到困惑的事情。这是我正在调用的方法:
@app.route('/facedata/<slug>', methods=["POST"])
def facedata(slug):
if request.method == "POST":
try:
post = Post.objects.get_or_404(slug=slug)
data = [float(item) for item in request.form.getlist('emotions[]')]
post.face_data.append(data)
post.save()
except:
traceback.print_exc(file=sys.stdout)
For a long time I was getting errors in here that would then be caught in the heroku logs. Currently there are no errors, implying that it doesn't reach the except loop, but even worse, there are still 500 errors. Specifically the 500 errors I get are:
很长一段时间以来,我在这里遇到错误,然后会在 heroku 日志中捕获这些错误。目前没有错误,这意味着它没有到达except循环,但更糟糕的是,仍然有500个错误。具体来说,我得到的 500 个错误是:
heroku[router]: at=info method=POST path=/facedata/StripedVuitton host=cryptic-mountain-6390.herokuapp.com fwd="18.111.90.180" dyno=web.2 connect=4ms service=39ms status=500 bytes=291
I'm sending these POST
requests via AJAX in this method:
我POST
在这种方法中通过 AJAX发送这些请求:
var slug = document.getElementById("hidden-slug").getAttribute("value");
data = {emotions: lRes};
$.ajax({
type: "POST",
data: data,
url: document.location.origin + "/facedata/" + slug,
success: function(){
console.log("Success!");
}
});
Quite honestly I just don't know how to continue debugging this problem. It doesn't make a lot of sense to me to be getting a traceback without an exception, but maybe I'm just being naive.
老实说,我只是不知道如何继续调试这个问题。毫无例外地获得回溯对我来说没有多大意义,但也许我只是天真。
I'm using mongoengine on top of MongoHQ on Heroku if that's relevant.
如果相关,我将在 Heroku 上的 MongoHQ 之上使用 mongoengine。
采纳答案by Slater Victoroff
After beating my head against this some more I finally figured it out thanks to the awesome people on the pocoo google group (I have since learned that there is a separate list for flask). Firstly, I needed to turn on the PROPAGATE_EXCEPTIONS
option in my app configuration (http://flask.pocoo.org/docs/config/#builtin-configuration-values).
多亏了 pocoo google 组中的优秀人员(我后来了解到有一个单独的烧瓶列表),我终于明白了这一点。首先,我需要PROPAGATE_EXCEPTIONS
在我的应用程序配置(http://flask.pocoo.org/docs/config/#builtin-configuration-values)中打开该选项。
After that was done I realized there was an issue with not returning a response from a view function, which Flask interpreted this method as. Since that was the case, this issue was resolved by just adding:
完成之后,我意识到不从视图函数返回响应是一个问题,Flask 将此方法解释为。既然是这样,只需添加以下内容即可解决此问题:
return jsonify(result={"status": 200})
To the end of the try
block. I hope this helps someone in a similar situation in the future.
到try
块的末尾。我希望这可以帮助将来遇到类似情况的人。
回答by Vito
I had the same issue, but the underlying cause was different than the one in Slater's response:
我有同样的问题,但根本原因与斯莱特回应中的不同:
I was muting the logger that the stack trace goes to. Be sure that you are not filtering out the flask.app
logger, which is where the exception stack traces go to (note that this is different than the flask server's informational logs, named app.api
).
我正在静音堆栈跟踪所指向的记录器。确保您没有过滤掉flask.app
记录器,这是异常堆栈跟踪去的地方(请注意,这与 Flask 服务器的信息日志不同,名为app.api
)。