在django模板中具有列表格式值的呈现字典

在django模板中具有列表格式值的呈现字典,django,django-forms,django-templates,Django,Django Forms,Django Templates,我的字典格式如下: uri_dict :{ "152.2.3.4" : ["/v1/draft" , 2], "172.31.13.12" : ["/v1/url" , 34] } 我想以表格格式在django模板中呈现此内容: {% for keys, value in url_dict.items %} <tr border = 1px black> <th>{{ keys

我的字典格式如下:

uri_dict :{
"152.2.3.4" : ["/v1/draft" , 2],
"172.31.13.12" : ["/v1/url" , 34]
}
我想以表格格式在django模板中呈现此内容:

{% for keys, value in url_dict.items %}
                      <tr border = 1px black> 
                        <th>{{ keys }}</th>
                        <td> {{ value[0] }} </td>      <!--requires value[0]  but not working -->
                        <td>{{ value[1]}} </td>   <!--not working -->
                        </tr>
                        {% endfor %} 
{%用于键,url中的值\u dict.items%}
{{keys}}
{{value[0]}
{{value[1]}
{%endfor%}
请给我提供任何解决方案---如何在模板中迭代列表值?
如何迭代列表值要访问django模板上的数组元素,必须将它们称为elem.attribute

在您的例子中,value.0和value.1

{% for keys, value in url_dict.items %}
<tr border = 1px black> 
    <th>{{ keys }}</th>
    <td>{{ value.0 }}</td>   <!--requires value[0]  but not working -->
    <td>{{ value.1 }}</td>   <!--not working -->
</tr>
{% endfor %} 
{%用于键,url中的值\u dict.items%}
{{keys}}
{{value.0}}
{{value.1}}
{%endfor%}
本页可帮助您:


希望这有帮助,

要访问django模板上的数组元素,必须将它们称为elem.attribute

在您的例子中,value.0和value.1

{% for keys, value in url_dict.items %}
<tr border = 1px black> 
    <th>{{ keys }}</th>
    <td>{{ value.0 }}</td>   <!--requires value[0]  but not working -->
    <td>{{ value.1 }}</td>   <!--not working -->
</tr>
{% endfor %} 
{%用于键,url中的值\u dict.items%}
{{keys}}
{{value.0}}
{{value.1}}
{%endfor%}
本页可帮助您:


希望这有帮助,

如果字典的每个值上总是只有两个项目,即列表,那么

{% for keys, value in url_dict.items %}
     <tr border = 1px black> 
         <th>{{ keys }}</th>
             <td> {{value.0}}</td>
             <td> {{value.1}}</td>
     </tr>
{% endfor %} 
{%用于键,url中的值\u dict.items%}
{{keys}}
{{value.0}}
{{value.1}}
{%endfor%}
如果列表中可以有任意数量的项目,则只需为每个项目循环:

{% for keys, value in url_dict.items %}
     <tr border = 1px black> 
         <th>{{ keys }}</th>
         {% for eachval in value %}
             <td> {{ eachval}}</td>
         {% endfor %}
     </tr>
{% endfor %} 
{%用于键,url中的值\u dict.items%}
{{keys}}
{值%%中每个值的%s}
{{eachval}}
{%endfor%}
{%endfor%}

如果字典的每个值上始终只有两项,即列表,则

{% for keys, value in url_dict.items %}
     <tr border = 1px black> 
         <th>{{ keys }}</th>
             <td> {{value.0}}</td>
             <td> {{value.1}}</td>
     </tr>
{% endfor %} 
{%用于键,url中的值\u dict.items%}
{{keys}}
{{value.0}}
{{value.1}}
{%endfor%}
如果列表中可以有任意数量的项目,则只需为每个项目循环:

{% for keys, value in url_dict.items %}
     <tr border = 1px black> 
         <th>{{ keys }}</th>
         {% for eachval in value %}
             <td> {{ eachval}}</td>
         {% endfor %}
     </tr>
{% endfor %} 
{%用于键,url中的值\u dict.items%}
{{keys}}
{值%%中每个值的%s}
{{eachval}}
{%endfor%}
{%endfor%}

有几个选项:

<td>{{ value.0 }}</td>
<td>{{ value.1 }}</td>

{% for item in value %}
    <td>{{ item }}</td>
{% endfor}
{{value.0}
{{value.1}}
{值%%中的项的%1}
{{item}}
{%endfor}
我的评论(以及后面的其他答案)中涵盖了这两个问题。对于长度为2的列表,您还可以:

<td>{{ value|first }}</td>
<td>{{ value|last }}</td>
{{value | first}
{{value | last}}

有几个选项:

<td>{{ value.0 }}</td>
<td>{{ value.1 }}</td>

{% for item in value %}
    <td>{{ item }}</td>
{% endfor}
{{value.0}
{{value.1}}
{值%%中的项的%1}
{{item}}
{%endfor}
我的评论(以及后面的其他答案)中涵盖了这两个问题。对于长度为2的列表,您还可以:

<td>{{ value|first }}</td>
<td>{{ value|last }}</td>
{{value | first}
{{value | last}}

尝试
value.0
value.1
等等(或者只对
循环值执行
)。尝试
value.0
value.1
等等(或者只对
循环值执行
)。