Flask 应用程序无一例外地引发 500 错误 [英] Flask app raises a 500 error with no exception

查看:31
本文介绍了Flask 应用程序无一例外地引发 500 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在反对 Flask 中的这种方法,虽然我现在似乎正在取得进展,但我刚刚发生了一件让我困惑不已的事情.这是我正在调用的方法:

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:

@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)

很长一段时间以来,我在这里遇到错误,然后会在 heroku 日志中捕获这些错误.目前没有错误,这意味着它没有到达except循环,但更糟糕的是,仍然有500个错误.具体来说,我得到的 500 个错误是:

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[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

我使用这种方法通过 AJAX 发送这些 POST 请求:

I'm sending these POST requests via AJAX in this method:

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.

如果相关,我将在 Heroku 上的 MongoHQ 之上使用 mongoengine.

I'm using mongoengine on top of MongoHQ on Heroku if that's relevant.

推荐答案

在我对这个问题再三打击之后,我终于想通了,这要感谢 pocoo google 组中的出色人员(我后来了解到有一个单独的烧瓶列表).首先,我需要在我的应用程序配置中打开 PROPAGATE_EXCEPTIONS 选项(http://flask.pocoo.org/docs/config/#builtin-configuration-values).

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).

完成后,我意识到有一个问题,即没有从视图函数返回响应,Flask 将此方法解释为.既然是这样,这个问题就通过添加解决了:

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:

return jsonify(result={"status": 200})

try 块的末尾.我希望这可以帮助将来遇到类似情况的人.

To the end of the try block. I hope this helps someone in a similar situation in the future.

这篇关于Flask 应用程序无一例外地引发 500 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆