Python Flask,TypeError:'dict'对象不可调用

时间:2015-12-03 04:22:46

标签: python json flask

有一个似乎很常见的问题,但我已经完成了我的研究,并没有看到它在任何地方都被完全重建。当我打印json.loads(rety.text)时,我看到了我需要的输出。然而,当我打电话给return时,它会向我显示这个错误。有任何想法吗?非常感谢帮助,谢谢。我正在使用Flask MethodHandler

class MHandler(MethodView):
    def get(self):
        handle = ''
        tweetnum = 100

        consumer_token = '' 
        consumer_secret = ''
        access_token = '-'
        access_secret = ''

        auth = tweepy.OAuthHandler(consumer_token,consumer_secret)
        auth.set_access_token(access_token,access_secret)

        api  = tweepy.API(auth)

        statuses = api.user_timeline(screen_name=handle,
                          count= tweetnum,
                          include_rts=False)

        pi_content_items_array = map(convert_status_to_pi_content_item, statuses)
        pi_content_items = { 'contentItems' : pi_content_items_array }

        saveFile = open("static/public/text/en.txt",'a') 
        for s in pi_content_items_array: 
            stat = s['content'].encode('utf-8')
            print stat

            trat = ''.join(i for i in stat if ord(i)<128)
            print trat
            saveFile.write(trat.encode('utf-8')+'\n'+'\n')

        try:
            contentFile = open("static/public/text/en.txt", "r")
            fr = contentFile.read()
        except Exception as e:
            print "ERROR: couldn't read text file: %s" % e
        finally:
            contentFile.close()
        return lookup.get_template("newin.html").render(content=fr) 

    def post(self):
        try:
            contentFile = open("static/public/text/en.txt", "r")
            fd = contentFile.read()
        except Exception as e:
            print "ERROR: couldn't read text file: %s" % e
        finally:
                contentFile.close()
        rety = requests.post('https://gateway.watsonplatform.net/personality-insights/api/v2/profile', 
                auth=('---', ''),
                headers = {"content-type": "text/plain"},
                data=fd
            )

        print json.loads(rety.text)
        return json.loads(rety.text)


    user_view = MHandler.as_view('user_api')
    app.add_url_rule('/results2', view_func=user_view, methods=['GET',])
    app.add_url_rule('/results2', view_func=user_view, methods=['POST',])

以下是Traceback(请记住上面打印的结果):

Traceback (most recent call last):
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/flask/app.py", line 1577, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/werkzeug/wrappers.py", line 841, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "/Users/RZB/anaconda/lib/python2.7/site-packages/werkzeug/test.py", line 867, in run_wsgi_app
    app_rv = app(environ, start_response)

5 个答案:

答案 0 :(得分:54)

Flask only expects views to return a response-like object.这意味着Response,一个字符串或一个描述正文,代码和标题的元组。你正在归还一个dict,这不是其中之一。由于您正在返回JSON,因此返回主体中包含JSON字符串且内容类型为application/json的响应。

return app.response_class(rety.content, content_type='application/json')

在您的示例中,您已经拥有一个JSON字符串,即您所做请求返回的内容。但是,如果要将Python结构转换为JSON响应,请使用jsonify

data = {'name': 'davidism'}
return jsonify(data)

在幕后,Flask是一个WSGI应用程序,它希望传递可调用对象,这就是你得到那个特定错误的原因:dict不可调用而且Flask不知道如何把它转换为

答案 1 :(得分:14)

使用Flask.jsonify函数返回数据。

from flask import jsonify 
# ...
return jsonify(data)

答案 2 :(得分:4)

如果从Flask视图返回data, status, headers元组,当数据已经是响应对象时,Flask当前会忽略状态代码和content_type标头,例如jsonify返回的内容。

这不会设置内容类型标题:

headers = {
    "Content-Type": "application/octet-stream",
    "Content-Disposition": "attachment; filename=foobar.json"
}
return jsonify({"foo": "bar"}), 200, headers

相反,使用flask.json.dumps生成数据(这是jsonfiy内部使用的数据)。

from flask import json

headers = {
    "Content-Type": "application/octet-stream",
    "Content-Disposition": "attachment; filename=foobar.json"
}
return json.dumps({"foo": "bar"}), 200, headers

或使用响应对象:

response = jsonify({"foo": "bar"})
response.headers.set("Content-Type", "application/octet-stream")
return response

但是,如果您希望按字面意义执行这些示例显示并将JSON数据作为下载提供,请改用send_file

from io import BytesIO
from flask import json
data = BytesIO(json.dumps(data))
return send_file(data, mimetype="application/json", as_attachment=True, attachment_filename="data.json")

答案 3 :(得分:1)

对于烧瓶版本1.1.0,现在您可以返回字典

烧瓶会自动将其转换为json响应。

https://flask.palletsprojects.com/en/1.1.x/quickstart/#apis-with-json https://flask.palletsprojects.com/en/1.1.x/changelog/#version-1-1-0

答案 4 :(得分:0)

这不是尝试对响应进行json处理,而是有效。

return response.content