我的Django视图:
def hub(request):
context = {}
hub_id = [value['id'] for value in hub_data['data']]
hub_name = [value['attributes']['name'] for value in hub_data['data']]
hub_url = [value['links']['self']['href'] for value in hub_data['data']]
nested_dict = dict(zip(hub_name, map(list, zip(hub_id, hub_url))))
context ['rows'] = nested_dict
return render(request, 'connector/hub.html', context)上下文的“行”结果如下:
{'rows': {hub_name1 : ['hub_id1', 'hub_url1'],{hub_name2 : ['hub_id2', 'hub_url2'], etc.. }我正试图传递一个HTML表,如下所示:
<th scope="col">Hub Name</th>
<th scope="col">Hub ID</th>
<th scope="col">Hub URL</th>我的桌子看起来像这样:
<tbody>
{% for key, value in rows.items %}
<tr>
<td> {{ key }}</td>
<td> {{ value }}</td> **//how do i just get hub_id here**
<td> Don't know what to do here to get: hub_url </td>
</tr>
{% endfor %}
</tbody>但是我想添加另一个-tag来填充hub_url。如何提取hub_id数据并将其添加到列: Hub ID并提取hub_url并将其添加到列集线器URL。
任何帮助都将不胜感激!
发布于 2020-08-02 18:48:25
您可以将数据传递给模板,而无需转换它。
return render(request, 'connector/hub.html', {'data': hub_data['data']})然后使用“点”模板语法查找每一行的属性。
<tbody>
{% for row in data %}
<tr>
<td>{{ row.attributes.name }}</td>
<td>{{ row.id }}</td>
<td>{{ row.links.self.href }}</td>
</tr>
{% endfor %}
</tbody>https://stackoverflow.com/questions/63219734
复制相似问题