现在,我有一个字典,在那里我存储了每个元素及其宽度。我将它与Django一起传递到一个HTML模板,在那里我用for循环呈现每个dict对象。但是,我需要能够使用Django模板中的内容设置“div”的宽度。基本上,如果dict中有“100%”,那么我希望相应的div也有100%的宽度。下面是我尝试的代码,但它不起作用,在控制台中,它显示“无效属性值”
Python代码:
objects={"key": "100%", "key2": "90%"}
return render(request, "website/updates.html", {
"objects": objects
})
网站/updates.html
{% for item, width in objects.items %}
<div style="width: {{width}}">
Other irrelevant code here
</div>
{% endfor %}
试试这样:
<div style="background-color: black; width: {{ dict.key }}">
这样,您就不必在dictionary对象上循环
相关问题 更多 >
编程相关推荐