【问题标题】:Django: Printing a queryset dynamically in the templateDjango:在模板中动态打印查询集
【发布时间】:2009-11-02 19:47:57
【问题描述】:

如何打印“fields_i_want”中指定的列,而不是在模板代码中硬编码列名?

# Let's say I have this in my view:
foo = Foo.objects.filter(some_field='bar').select('field1', 'field2', 'field3')
fields_i_want = ['field1', 'field2']

# Then in the template I want to do something like this:
<TABLE id="some_id">    
    <TBODY>
        {% for row in some_var.foo %}
        <tr>
         {% for value in another_var.fields_i_want %}
            <td>{{ row[value] }}</td>
         {% endfor %}
        </tr>
        {% endfor %}
    </TBODY>
</TABLE>

# Instead of doing this:
<TABLE id="some_id">    
    <TBODY>
        {% for row in some_var.foo %}
        <tr>
            <td>{{ row.field1 }}</td>
            <td>{{ row.field2 }}</td>
        </tr>
        {% endfor %}
    </TBODY>
</TABLE>

【问题讨论】:

    标签: django dynamic templates django-queryset


    【解决方案1】:

    【讨论】:

    • 谢谢!!这正是我想要的:)
    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    相关资源
    最近更新 更多