从循环中返回所有数据(多次返回以覆盖页面)

2024-04-20 02:57:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个应用程序开发一个API,但是我想从我的sql查询中输出多个json对象,但是如果我使用多个返回,页面就会被覆盖

我的代码是:

@app.route('/api/location')
@support_jsonp
def get_locations():
    d = {}
    for i, row in enumerate(locations):
            l = []
            for col in range(0, len(row)):
                    l.append(row[col])
            d[i] = l
            for s in range(0, len(d)):
                    db_questionid = d[s][0]
                    db_title = d[s][1]
                    db_text = d[s][2]
                    db_long = d[s][3]
                    db_lat = d[s][4]
                    db_completed = d[s][5]
                    db_image = d[s][6]
                    return jsonify({'id': db_questionid,
                    'title': db_title,
                    'text': db_text.decode("ISO-8859-1"),
                    'long': db_long,
                    'lat': db_lat,
                    'completed': db_completed,
                    'image': db_image})

你将如何修复它?我真的卡住了

提前感谢,

乔迪


Tags: textinimagefordblentitlerange
1条回答
网友
1楼 · 发布于 2024-04-20 02:57:31

我不得不将stream_与_上下文一起使用&;这样我才能做出回应

例如:

@app.route('/api/location')
@support_jsonp
def get_locations():
       def generate():
            d = {}
            for i, row in enumerate(locations):
                    l = []
                    for col in range(0, len(row)):
                            l.append(row[col])
                    d[i] = l
                    for s in range(0, len(d)):
                            db_questionid = d[s][0]
                            db_title = d[s][1]
                            db_text = d[s][2]
                            db_long = d[s][3]
                            db_lat = d[s][4]
                            db_completed = d[s][5]
                            db_image = d[s][6]
                            yield jsonify({'id': db_questionid,
                            'title': db_title,
                            'text': db_text.decode("ISO-8859-1"),
                            'long': db_long,
                            'lat': db_lat,
                            'completed': db_completed,
                            'image': db_image}).data
          return Response(stream_with_context(generate()))

相关问题 更多 >