Flask应用程序引发500错误,也不例外 [英] Flask app raises a 500 error with no exception

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

问题描述

我一直在Flask里碰到这个方法一段时间,而现在看来我正在取得进步,我刚刚发生过一些让我无法忍受的事情。这是我打电话的方法:

  @ app.route('/ facedata /< slug>', [POST])
def facedata(slug):
if request.method ==POST:
try:
post = Post.objects.get_or_404(slug = slug)
data = [float(item)for request in request.form.getlist('emotions []']]
post.face_data.append(data)
post.save()
除了
traceback.print_exc(file = sys.stdout)

很长一段时间,我在这里收到错误,然后将被抓到英雄的日志。目前没有错误,这意味着它没有达到除了循环,但更糟糕的是,还有500个错误。具体来说,我得到的500个错误是:

  heroku [router]:at = info method = POST path = / facedata / StripedVuitton host = cryptic-mountain-6390.herokuapp.com fwd =18.111.90.180dyno = web.2 connect = 4ms service = 39ms status = 500 bytes = 291 

我通过AJAX发送这些 POST 请求:

  var slug = document.getElementById(hidden-slug)。getAttribute(value); 
data = {emotions:lRes};
$ .ajax({
type:POST,
data:data,
url:document.location.origin +/ facedata /+ slug,
成功:function(){
console.log(Success!);
}
});

老实说,我只是不知道如何继续调试这个问题。对我来说,没有任何异常的追溯并没有什么意义,但也许我只是天真。



我正在使用mongoengine如果相关的话,在Heroku上的MongoHQ的顶部。

解决方案

在我打了头后,我终于明白了,感谢pocoo google组的真棒人(以前我已经知道有一个单独的烧瓶列表)。首先,我需要在我的应用配置中打开 PROPAGATE_EXCEPTIONS 选项( http://flask.pocoo.org/docs/config/#builtin-configuration-values )。



完成之后我意识到没有从视图函数返回响应的问题,Flask将此方法解释为。既然是这样的话,这个问题已经解决了:

  return jsonify(result = {status:200} )

尝试块的末尾。我希望这可以帮助未来有类似情况的人。


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)

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

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.

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

解决方案

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

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

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

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

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