How to assign template tag-value in loop?

时间:2022-01-21 20:29:51

I have an HTML snippet which I like to re-use many times on a page. For instance

我有一个HTML代码段,我喜欢在页面上多次重复使用。例如

<div id="custom">
    <p>{{ object_name }}</p>
    <p>{{ object_value }}</p>
    <p>Static text</p>
</div>

How can I include this snippet on a page/template, where the values of the template tags or assigned dynamically?

如何在页面/模板上包含此代码段,其中模板标记的值是动态分配的?

Currently I work with this solution:

目前我使用这个解决方案:

<div class="custom">
    <p>{{ object_name_one }}</p>
    <p>{{ object_value_one }}</p>
    <p>Static text</p>
</div>
<div class="custom">
    <p>{{ object_name_two }}</p>
    <p>{{ object_value_two }}</p>
    <p>Static text</p>
</div>
<div class="custom">
    <p>{{ object_name_three }}</p>
    <p>{{ object_value_three }}</p>
    <p>Static text</p>
</div>

With a dictionary containing the respective values I pass in the view of this app. Instead of a list of objects containing the values, I assigned all values, for instance

使用包含相应值的字典,我在此应用程序的视图中传递。例如,我分配了所有值,而不是包含值的对象列表

{ ..., object_name_three = l[2].name, ... }

1 个解决方案

#1


0  

{% for item in dir %}
    <div class="hi">
        {{ item.one }}
        {{ item.two }}
    </div>
{% endfor %}

and in your context:

在您的背景下:

context = {'dir':[{'one':'uno', 'two':'no'}, {'one':'uo', 'two':'sino'}]}

tell me if that works or I misunderstood

告诉我这是否有效或我误解了

#1


0  

{% for item in dir %}
    <div class="hi">
        {{ item.one }}
        {{ item.two }}
    </div>
{% endfor %}

and in your context:

在您的背景下:

context = {'dir':[{'one':'uno', 'two':'no'}, {'one':'uo', 'two':'sino'}]}

tell me if that works or I misunderstood

告诉我这是否有效或我误解了