[英]Django nested dictionary to display in templates
我有一本看起来像这样的字典
dict = {
'https://i.redd.it/4d87ifm2mch01.jpg': 'https://reddit.com/7yv1d8',
'https://i.redd.it/ru0bq0jpr9h01.jpg': 'https://reddit.com/7ys0l3',
'videos': {'https://gfycat.com/ifr/selfassuredinfinitehochstettersfrog'},..
}
'videos'
将值存储为一个集合,所以我不会有重复的 url,这很方便。 但是我遇到了一个问题,如何在模板中显示嵌套字典? 我现在就这样
{% for keys,values in data.items%}
{% for i,j in keys.items %}
<iframe src='{{j}}' frameborder='0' scrolling='no' allowfullscreen width='300' height='300'></iframe>
<a class="thumbnail" href="{{values}}"><img src="{{keys}}" width="100px" height="66px" border="1" /><span><img src="{{keys}}" /><br />whatever.</span></a>
<br />
我希望将'videos'
值作为图像存储在<iframe>
和其他文件中。
在keys
上调用.items
可能会引发错误,因为它们是字符串,我不知道保存了什么data
,但是假设它在您的视图中发送到模板,例如:
return render(request, "myapp/index.html", {"data": dict})
然后您可以检查key
等于"videos"
,并遍历该集合:
{% for key, value in data.items %}
{% ifequal key "videos" %}
<iframe ...>
{% for url in value %}
<a class="thumbnail" href="{{ url }}">{{ url }}</a>
{% endfor %}
</iframe>
{% endifequal %}
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.