在Django中使用JSON。添加数据并在模板中解析

时间:2019-05-28 07:34:10

标签: python json django

我有一个将数据添加到JSON的代码:

for pair in pairs:
    for timeframe in timeframes:
        klines = Kline.objects.filter(timeframe=timeframe, pair=pair, closed = True).order_by('-open_time')[:10]
        list_pair_data = []
        for kline in klines:
            kline_data = []
            kline_data.append(kline.price_open)
            kline_data.append(kline.price_close)
            list_pair_data.append(kline_data)

        pair_data = json.dumps(
            {'pair':pair.title,
            'timeframe':timeframe.title,
            'data': list_pair_data})
        all_data.append(pair_data)

return render(request, 'indicators/indicators_list.html',{'all_data':all_data})

我在模板中显示它:

  {% for item in all_data %}
    <p>{{ item }}</p>

  {% endfor %}

我在页面中看到的内容:

{"pair": "BTCUSDT", "timeframe": "1h", "data": [[7654.09, 7660.98], [7619.18, 7654.96], [7636.3, 7620.76], [7627.8, 7636.28], [7714.04, 7628.43], [7666.53, 7715.02], [7815.42, 7666.53], [7901.2, 7815.42], [7906.9, 7901.69], [7880.55, 7907.71]]}

{"pair": "BTCUSDT", "timeframe": "4h", "data": [[7627.8, 7660.98], [7901.2, 7628.43], [7939.96, 7901.69], [7802.95, 7939.6], [7881.19, 7802.31], [7895.35, 7882.22], [7904.48, 7895.17], [7966.44, 7904.87], [7890.62, 7966.45], [7847.92, 7890.49]]}

{"pair": "BTCUSDT", "timeframe": "1d", "data": [[7904.48, 7628.43], [7937.16, 7904.87], [8147.94, 7938.15], [7257.32, 8148.48], [7355.28, 7257.45], [7868.67, 7355.26], [8169.08, 7866.59], [7945.26, 8169.87], [7795.62, 7947.56], [6968.24, 7790.71]]}

{"pair": "BTCUSDT", "timeframe": "15m", "data": [[7637.82, 7649.93], [7655.54, 7638.74], [7661.45, 7657.92], [7627.58, 7660.98], [7633.5, 7627.65], [7638.67, 7632.93], [7654.09, 7640.39], [7668.52, 7654.96], [7648.13, 7672.28], [7595.65, 7648.11]]}

{"pair": "ETHBTC", "timeframe": "1h", "data": [[0.031903, 0.031785], [0.03195, 0.031903], [0.031843, 0.031958], [0.031952, 0.031843], [0.03189, 0.031953], [0.031696, 0.031879], [0.031659, 0.03169], [0.03198, 0.031659], [0.031966, 0.03198], [0.031881, 0.031964]]}

{"pair": "ETHBTC", "timeframe": "4h", "data": [[0.031952, 0.031785], [0.03198, 0.031953], [0.032143, 0.03198], [0.032282, 0.032142], [0.032429, 0.032288], [0.03228, 0.032433], [0.032047, 0.032283], [0.03205, 0.03206], [0.031724, 0.03205], [0.031801, 0.031722]]}

但是我该如何获取格式:

BTCUSDT - 1h - [7654.09, 7660.98], [7619.18, 7654.96], [7636.3, 7620.76], [7627.8, 7636.28], [7714.04, 7628.43], [7666.53, 7715.02], [7815.42, 7666.53], [7901.2, 7815.42], [7906.9, 7901.69], [7880.55, 7907.71]

BTCUSDT - 1d - [7654.09, 7660.98], [7619.18, 7654.96], [7636.3, 7620.76], [7627.8, 7636.28], [7714.04, 7628.43], [7666.53, 7715.02], [7815.42, 7666.53], [7901.2, 7815.42], [7906.9, 7901.69], [7880.55, 7907.71]

.....

最佳解决方案?我需要:

BTCUSDT:

1h - [7654.09, 7660.98], [7619.18, 7654.96], [7636.3, 7620.76], [7627.8, 7636.28], [7714.04, 7628.43], [7666.53, 7715.02], [7815.42, 7666.53], [7901.2, 7815.42], [7906.9, 7901.69], [7880.55, 7907.71]

1d - [7654.09, 7660.98], [7619.18, 7654.96], [7636.3, 7620.76], [7627.8, 7636.28], [7714.04, 7628.43], [7666.53, 7715.02], [7815.42, 7666.53], [7901.2, 7815.42], [7906.9, 7901.69], [7880.55, 7907.71]


ETHBTC:

1h - [0.031903, 0.031785], [0.03195, 0.031903], [0.031843, 0.031958], [0.031952, 0.031843], [0.03189, 0.031953], [0.031696, 0.031879], [0.031659, 0.03169], [0.03198, 0.031659], [0.031966, 0.03198], [0.031881, 0.031964]

1d - [0.031903, 0.031785], [0.03195, 0.031903], [0.031843, 0.031958], [0.031952, 0.031843], [0.03189, 0.031953], [0.031696, 0.031879], [0.031659, 0.03169], [0.03198, 0.031659], [0.031966, 0.03198], [0.031881, 0.031964]

我不明白,问题在于如何将数据添加到JSON或如何解析。或两者兼有。

UPD: 我尝试这样做:

        pair_data = [pair.title,timeframe.title,list_pair_data]
        all_data.append(pair_data)

模板:

{% for item in all_data %}

    {% for val in item %}
    {{ val}}
    {% endfor %}
    <p>---</p>
  {% endfor %}

但是在这种情况下,我无法直接获得结对标题。而且,我的想法是,主要问题是如何将JSON以以下格式放置在模板中:

 {pairs:[
  {"pair": "BTCUSDT", "timeframe": "1h", "data": [[7654.09, 7660.98], [7619.18, 7654.96], [7636.3, 7620.76], [7627.8, 7636.28], [7714.04, 7628.43], [7666.53, 7715.02], [7815.42, 7666.53], [7901.2, 7815.42], [7906.9, 7901.69], [7880.55, 7907.71]]}

  {"pair": "BTCUSDT", "timeframe": "4h", "data": [[7627.8, 7660.98], [7901.2, 7628.43], [7939.96, 7901.69], [7802.95, 7939.6], [7881.19, 7802.31], [7895.35, 7882.22], [7904.48, 7895.17], [7966.44, 7904.87], [7890.62, 7966.45], [7847.92, 7890.49]]}

  {"pair": "BTCUSDT", "timeframe": "1d", "data": [[7904.48, 7628.43], [7937.16, 7904.87], [8147.94, 7938.15], [7257.32, 8148.48], [7355.28, 7257.45], [7868.67, 7355.26], [8169.08, 7866.59], [7945.26, 8169.87], [7795.62, 7947.56], [6968.24, 7790.71]]]}}

0 个答案:

没有答案