问题描述
我有一个返回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从一个视图获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!