1

我正在尝试按照以下片段在 Flask 中为 JSON 编写视图渲染装饰器:http: //flask.pocoo.org/snippets/18/

问题是我需要序列化为 JSON、GAE 模型,而jsonify函数对此不起作用。为此,我使用以下代码:http ://blog.worldmaker.net/2009/dec/08/simple-app-engine-json-serialization-snippet/

与上面代码片段中的 render_html 示例类似,我编写了以下代码:

def render_json(result):
  def wrapped(*args, **kwargs):
    json_string = json.dumps(result, cls=GaeEncoder)
    return app.response_class(response=json_string, mimetype='application/json')
  return wrapped

问题是我调用了一个用我的新装饰器装饰的函数,我收到以下错误:

AttributeError: 'Response' object has no attribute 'next'

谁能指出这里发生了什么?谢谢!

马科斯

编辑:这是回溯

Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 208, in Handle
for chunk in result:
File "/Users/mchicote/Documents/projects/schoolnetwork/code/SchoolNetwork/sn/../lib/flaskext/gae_mini_profiler/profiler.py", line 286, in __call__
yield self.prof.runcall(result.next)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.py", line 149, in runcall
return func(*args, **kw)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/appstats/recording.py", line 1284, in appstats_wsgi_wrapper
result = app(environ, appstats_start_response)
File "/Users/mchicote/Documents/projects/schoolnetwork/code/SchoolNetwork/sn/../lib/flaskext/gae_mini_profiler/profiler.py", line 269, in wrapped_appstats_app
return old_app(environ, start_response)
File "/Users/mchicote/Documents/projects/schoolnetwork/code/SchoolNetwork/sn/../lib/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/mchicote/Documents/projects/schoolnetwork/code/SchoolNetwork/sn/../lib/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/Users/mchicote/Documents/projects/schoolnetwork/code/SchoolNetwork/sn/../lib/flask/app.py", line 1361, in full_dispatch_request
response = self.make_response(rv)
File "/Users/mchicote/Documents/projects/schoolnetwork/code/SchoolNetwork/sn/../lib/flask/app.py", line 1450, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/Users/mchicote/Documents/projects/schoolnetwork/code/SchoolNetwork/sn/../lib/werkzeug/wrappers.py", line 711, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/Users/mchicote/Documents/projects/schoolnetwork/code/SchoolNetwork/sn/../lib/werkzeug/wrappers.py", line 55, in _run_wsgi_app
return _run_wsgi_app(*args)
File "/Users/mchicote/Documents/projects/schoolnetwork/code/SchoolNetwork/sn/../lib/werkzeug/test.py", line 836, in run_wsgi_app
buffer.append(app_iter.next())
4

1 回答 1

0

知道了!以防万一有人觉得它有帮助......

第一个问题是我没有遵循上面片段中描述的完全相同的模式。当我尝试遵循该模式时,我收到以下错误:

TypeError: wrapped() takes exactly 1 argument (2 given)

我尝试改变模式,因为 render_json 不接受任何参数(与采用模板的 render_html 相比),并编写了上面描述的 render_json 函数(以及原始帖子中描述的问题)。在尝试了一些事情之后,我突然想到我可能没有正确处理参数数量错误,所以我回到使用原始模式编写render_json。我得到了以下功能:

def render_json(**defaults):
    def wrapped(result):
        variables = defaults.copy()
        variables.update(result)
        json_string = json.dumps(result, cls=GaeEncoder)
        return app.response_class(response=json_string, mimetype='application/json')                       
    return wrapped

回到参数数量错误,我尝试以不同的方式解决它。不知何故,我突然想到问题可能出在 render_json 函数的使用上。render_html 的装饰器用法如下所示:

@view(school_blueprint, '/', render_html('school/index.html'))

render_json 的用法如下所示:

@view(school_blueprint, '/api/list', render_json)

作为 Python 新手,我突然想到我实际上并没有调用 render_json 函数,而只是命名它。添加()解决了这个问题。新用法如下所示:

@view(school_blueprint, '/api/list', render_json())

无论如何,由于我是 Python 新手,我不知道技术解释,但在我看来,这是名称与调用问题。

谢谢你的帮助。

马科斯

于 2012-09-26T08:06:08.670 回答