通过从另一个视图调用JSON从一个视图获取 [英] Get json from one view by calling it from another view

查看:136
本文介绍了通过从另一个视图调用JSON从一个视图获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回JSON数据的视图。我想从另一个视图获取数据,所以我尝试从它调用JSON视图。但是,返回了 Response 而不是JSON数据。

  @ app.route('/ promoters /< int :id>',methods = ['GET'])
def get_promoter(id):
...

>>> get_promoter(3)
<响应440字节[200 OK]>


解决方案

在一个响应对象取决于它的类型(更多信息这些规则检出此处)。 json数据存储在 r.response 中,其中 r 响应 object。

更好的解决方案是创建一个单独的函数,它返回json数据,这个数据可以从两个不同的视图函数中调用。 >

I have a view that returns JSON data. I want to get that data from another view as well, so I tried calling the JSON view from it. However, a Response was returned rather than the JSON data. How can I call one view from another view and get the data?

@app.route('/promoters/<int:id>', methods=['GET'])
def get_promoter(id):
    ...

>>> get_promoter(3)
<Response 440 bytes [200 OK]>

解决方案

The decorator of a view-function can convert your return-value in a Response object depending on it's type (more information about these rules check out here). The json data is stored in r.response where r is the Response object.

The better solution is to create a separate function, which returns the json data, which can be called from two different view-functions.

这篇关于通过从另一个视图调用JSON从一个视图获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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