如何在views.py中重复此结构?

时间:2014-08-13 14:39:22

标签: python django loops python-2.7 django-views

我是python的新手,正在使用python-weather-api。我正在尝试将此JSON结构转换为list(这是我在Java中所知道的名称,如果不是,则抱歉)。

'forecasts': [   {   'code': u'34',
                         'date': u'27 Mar 2013',
                         'day': u'Wed',
                         'high': u'9',
                         'low': u'2',
                         'text': u'Mostly Sunny'},
                     {   'code': u'28',
                         'date': u'28 Mar 2013',
                         'day': u'Thu',
                         'high': u'10',
                         'low': u'2',
                         'text': u'Mostly Cloudy'},
                     {   'code': u'30',
                         'date': u'29 Mar 2013',
                         'day': u'Fri',
                         'high': u'11',
                         'low': u'3',
                         'text': u'Partly Cloudy'},
                     {   'code': u'30',
                         'date': u'30 Mar 2013',
                         'day': u'Sat',
                         'high': u'11',
                         'low': u'3',
                         'text': u'Partly Cloudy'},
                     {   'code': u'28',
                         'date': u'31 Mar 2013',
                         'day': u'Sun',
                         'high': u'10',
                         'low': u'7',
                         'text': u'Mostly Cloudy'}],

要访问第一个值,例如我正在执行此操作:

forecast = {
      'day_0': yahoo_result['forecasts'][0]['day'],
}

这就是我现在正在做的事情:

views.py
  forecast = {
          'day_0': yahoo_result['forecasts'][0]['day'],
          'text_0': yahoo_result['forecasts'][0]['text'],
          'high_0': yahoo_result['forecasts'][0]['high'],
          'low_0': yahoo_result['forecasts'][0]['low'],

          'day_1': yahoo_result['forecasts'][1]['day'],
          'text_1': yahoo_result['forecasts'][1]['text'],
          'high_1': yahoo_result['forecasts'][1]['high'],
          'low_1': yahoo_result['forecasts'][1]['low'],

          'day_2': yahoo_result['forecasts'][2]['day'],
          'text_2': yahoo_result['forecasts'][2]['text'],
          'high_2': yahoo_result['forecasts'][2]['high'],
          'low_2': yahoo_result['forecasts'][2]['low'],

          'day_3': yahoo_result['forecasts'][3]['day'],
          'text_3': yahoo_result['forecasts'][3]['text'],
          'high_3': yahoo_result['forecasts'][3]['high'],
          'low_3': yahoo_result['forecasts'][3]['low'],

          'day_4': yahoo_result['forecasts'][4]['day'],
          'text_4': yahoo_result['forecasts'][4]['text'],
          'high_4': yahoo_result['forecasts'][4]['high'],
          'low_4': yahoo_result['forecasts'][4]['low'],

    }

return render(request, 'index.html', {'weather':weather, 'forecast': forecast})

并在HTML中显示结果:

<div class="col-md-9">
    <ul>
        <li> <!-- start the loop right here -->
            <h2>{{ forecast.day_0 }}</h2>
            <i class=" ico-cloudy text-primary"></i>
            <div class="statistics"> low {{ forecast.low_0 }}</div>
            <div class="statistics"> high {{ forecast.high_0 }}</div>
        </li>
        <li>
            <h2>{{ forecast.day_1 }}</h2>
            <i class=" ico-cloudy text-primary"></i>
            <div class="statistics"> low {{ forecast.low_1 }}</div>
            <div class="statistics"> high {{ forecast.high_1 }}</div>
        </li>
        <li>
            <h2>{{ forecast.day_2 }}</h2>
            <i class=" ico-cloudy text-primary"></i>
            <div class="statistics"> low {{ forecast.low_2 }}</div>
            <div class="statistics"> high {{ forecast.high_2 }}</div>
        </li>
        <li>
            <h2>{{ forecast.day_3 }}</h2>
            <i class=" ico-cloudy text-primary"></i>
            <div class="statistics"> low {{ forecast.low_3 }}</div>
            <div class="statistics"> high {{ forecast.high_3 }}</div>
        </li>
        <li>
            <h2>{{ forecast.day_4 }}</h2>
            <i class=" ico-cloudy text-primary"></i>
            <div class="statistics"> low {{ forecast.low_4 }}</div>
            <div class="statistics"> high {{ forecast.high_4 }}</div>
        </li>
    </ul>
</div>

但是我想将整个结构放在一个列表中以便能够在HTML文件中循环,我该怎么做?

1 个答案:

答案 0 :(得分:3)

实际上yahoo_result['forecasts']似乎是一个列表,你试过这个:

在您看来:

return render(request, 'index.html', {'weather':weather, 'forecast': yahoo_result['forecasts']})

在模板文件中:

<ul>
    {% for item in forecast %}
        <li>
            <h2>{{ item.day }}</h2>
            <i class="ico-cloudy text-primary"></i>
            <div class="statistics"> low {{ item.low }}</div>
            <div class="statistics"> high {{ item.high }}</div>
        </li>
    {% endfor %}
</ul>