如何使用 Django 变量设置 HTML 元素的宽度?

How do I set the width of a HTML element with a Django variable?

现在,我有一本字典,其中存储了每个元素及其宽度。我将它与 Django 一起传递给 HTML 模板,我在其中使用 for 循环呈现每个 dict 对象。但是,我需要能够使用 Django 模板中的内容设置 'div' 的宽度。所以基本上,如果我在字典中有“100%”,那么我希望相应的 div 也有 100% 的宽度。我试过的代码在下面,但它不起作用,并且在控制台中,它显示“无效的 属性 值”。

Python代码:

objects={"key": "100%", "key2": "90%"}
return render(request, "website/updates.html", {
    "objects": objects
})

website/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 }}">

这样您就不必遍历字典对象。