简体   繁体   English

Django:当值是生成器时,字典到HTML表

[英]Django: dictionary to HTML table, when values are generators

I have a dictionary containing scraped Airbnb listings data: 我有一本字典,其中包含刮擦的Airbnb列表数据:

all_data = {
                    'name' : self.detail('listing', 'name'),
                    'city' : self.detail('listing', 'city'),
                    'id' : self.detail('listing', 'id'),
                    'latitude' : self.detail('listing', 'lat'),
                    'longitude' : self.detail('listing', 'lng'),
                    'picture' : self.detail('listing', 'picture_url'),
                    'pictures' : self.detail('listing', 'picture_urls'),
                    'price' : self.detail('pricing_quote', 'rate', 'amount'),
                    'currency' : self.detail('pricing_quote', 'rate', 'currency')
                    }

That I am passing from Django view to template like this: 我从Django视图传递给模板,如下所示:

context = {'all_data':all_data}
return render(request, 'javascript/testjson.html', context)

The values in this dictionary are generators, yielding a specific listing detail. 该词典中的值是生成器,产生特定的清单详细信息。

How do I present this data in a HTML table form in the template? 如何在模板中以HTML表格形式显示此数据?

So far I have the following for displaying the headers, but I don't know how to extract the data from generators into columns below those headers: 到目前为止,我有以下显示标题的信息,但是我不知道如何将生成器中的数据提取到这些标题下的列中:

<table>
    <tr>
        {% for key, value in all_data.items %}
            <th>{{key}}</th>
        {% endfor %}
    </tr>
</table>

you can update the html like this 您可以像这样更新html

<table>
  <tr>
    {% for key in all_data.keys %}
        <th>{{key}}</th>
    {% endfor %}
  </tr>
  <tr>
    {% for value in all_data.values %}
        <td>
         {% for v in value %}
           {{v}}
         {% endfor %
       </td>
    {% endfor %}
  </tr>
</table>

this will work.... 这将工作...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM