简体   繁体   English

django - 在模板标签中显示 OrderedDict

[英]django - showing OrderedDict in templatetag

I have a OrderedDict and I need to show its key, value but I am unable to get its value, I have this dict = ([('sainbath', 'thesailor'), ('HELLO', 'WORLD')]), I have tried this我有一个 OrderedDict,我需要显示它的键值,但我无法获取它的值,我有这个 dict = ([('sainbath', 'thesailor'), ('HELLO', 'WORLD')]) ,我试过这个

{{object.specifications}}
      <ul>
        <li>{% for key in object.specifications %}
          {{key}}
          {%endfor%}

I am getting this output我得到这个输出

OrderedDict([('sainbath', 'thesailor'), ('HELLO', 'WORLD')]) OrderedDict([('sainbath', 'thesailor'), ('HELLO', 'WORLD')])

  • sainbath HELLO圣巴思你好

I am getting its key only not value, when I tried this当我尝试这个时,我只得到它的键而不是值

{{object.specifications}}
      <ul>
        <li>{% for key,value in object.specifications %}
          {{key}} : {{value}}
          {%endfor%}

it give me error它给了我错误

Need 2 values to unpack in for loop;需要 2 个值才能在 for 循环中解包; got 8. please tell me how can I get value? got 8. 请告诉我如何获得价值?

1) Use following in template: 1)在模板中使用以下内容:

{% for key,value in object.specifications.items %}
      {{key}}:{{value}}
{% endfor %}

OR

2)If you are rendering a template with the data as OrderedDict, you can use following approach for the same. 2)如果您使用 OrderedDict 的数据呈现模板,则可以使用以下方法。

return render_to_response('your_template.html',{'data': sorted(your_ordered_dict.iteritems())})

and in Template you can use the same as below:在模板中,您可以使用以下相同的内容:

{% for key, value in data %}
     {{key}}:{{value}}
{% endfor %}

Hope it will help you!!希望能帮到你!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM