我正在尝试在 Django 中创建一个模板。在该模板中我有:
dato
包含一个像这样的字典 {"Name":"Someone","Age":"23"}
和 propiedad
包含一个像这样的字典{"parameterName":"Name"}
。我可以创建 {{propiedad.parameterName}}
并获取值 Name
。之后,我想将该值存储在 buffer
中(直到现在还不存在),以便稍后 {{dato.buffer}}
获取 Someone
我将在 HTML 中显示它。我不知道该怎么做,有人可以帮助我吗?
代码片段:
<tbody>
{% for dato in atributo.datos %}
<tr>
{% for propiedad in atributo.propiedades %}
<td>
{# I would make the stuff here#}
{{ dato.buffer}}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
最佳答案
如果我正确地回答了你的问题,你需要编写自己的自定义 django template filter像这样的东西:
from django.template.defaulttags import register
...
@register.filter
def getparam(dictionary, key):
return dictionary.get(key)
并使用它将其放入您的 html 中:
{{ dato|getparam:propiedad }}
关于python - Django,在模板中创建变量并赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30889911/