python - Django,在模板中创建变量并赋值

标签 python django templates

我正在尝试在 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/

相关文章:

python - 如何找到可以将新项目插入排序列表并保持排序的索引?

python - 如何使用正则表达式匹配请求行?

Django 管理内联表单,具有只读旧值并允许添加新的内联值

python - Matplotlib 无对象 'Use' 或 '__version__'

django - 在 Amazon Elastic Beanstalk 上使用 Docker 部署 Django

创建其他类实例的 C++ 模板类

python - 如何在Python中跳过几行后读取json文件?

python - 当两个列表匹配python时返回空集

c# - 打开模板文件的多个副本(.Net、Excel.Interop)

c++ - 在 Variadic 模板类中使用 std::bind