如何在django模板中迭代可变长度列表?

时间:2014-01-25 11:46:39

标签: django django-templates

我有一个值列表,如:

list_of_values = ['clients':['add, view'], 'vendors': ['add', 'delete', 'change'], 'companies': ['add', 'view', 'delete', 'change']]

使用django模板标签我必须制作如下模板:

Activities    ADD      |  VIEW      |  CHANGE      |  DELETE
clients      checkbox    checkbox     checkbox       checkbox
vendors      checkbox    checkbox     checkbox       checkbox
companies    checkbox    checkbox     checkbox       checkbox

请让我知道如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

值列表看起来更像是我的字典,假设它是:

<table>
    <tr>
        <th>Activities</th>
        <th>ADD</th>
        <th>VIEW</th>
        <th>CHANGE</th>
        <th>DELETE</th>
    </tr>
    {% for key in list_of_values %}
        <tr>
            <td>{{ key }}</td>
            <td>
                {% if 'add' in list_of_values.key %}
                    <input type="checkbox" checked/>
                {% else %}
                    <input type="checkbox"/>
                {% endif %}
            </td>
            <td>
                {% if 'view' in list_of_values.key %}
                    <input type="checkbox" checked/>
                {% else %}
                    <input type="checkbox"/>
                {% endif %}
            </td>
            <td>
                {% if 'change' in list_of_values.key %}
                    <input type="checkbox" checked/>
                {% else %}
                    <input type="checkbox"/>
                {% endif %}
            </td>
            <td>
                {% if 'delete' in list_of_values.key %}
                    <input type="checkbox" checked/>
                {% else %}
                    <input type="checkbox"/>
                {% endif %}
           </td>
        </tr>        
    {% endfor %}
</table>