可以从django模板访问字典中的特定键值吗? [英] Can I access specific key values in dictionary from django template?

查看:285
本文介绍了可以从django模板访问字典中的特定键值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {%for key,values in choices.items%} 
< li> {{key}} - {{value}}< / li>
{%endfor%}

从python我有get()函数来获取值从一个特定的键。但是我找不到与django模板标签相对应的方法。所以我想知道这是可能吗?
我需要获取特定的值,因为使用循环在html源中添加了大量新行。



或者应该在视图之前处理输出发送给模板,哪种方法更好?

解决方案

可以使用 {{choices。 items.key}} 以访问特定的dict元素。



尽管HTML代码中没有任何理由关心空格;典型的最终用户没有真正的业务阅读,如果他好奇,他总是使用DOM查看器或通过HTML美化器运行它。


Is there any get() function for this instead?

{% for key, value in choices.items %} 
  <li>{{key}} - {{value}}</li>
{% endfor %}

From python I have the get() function to get values from a specific key. But I couldn't find a corresponding way to do that with django template tags. So I wonder is it possible? I need to get specific values since using loops adds a lot of new lines in the html source.

Or should take care of the output inside the view before sending it out to the template, which method is better?

解决方案

You can use {{ choices.items.key }} to access a specific dict element.

There is no reason to care about whitespace in the HTML code though; the typical end-user has no real business in reading it and if he's curious he an always use a DOM viewer or run it through a HTML beautifier.

这篇关于可以从django模板访问字典中的特定键值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆