Django模板访问嵌套数据

时间:2021-05-09 22:56:50

This seems silly, but I don't understand how Django Templates access nested data in Contexts. I can access the values of dictionaries nested in the context data structure with the . notation -- {{ aDictionary.i_am_a_key }} works fine. But if I try to iterate over a list of keys and get their value from that same dictionary, I get nothing. So

这看起来很傻,但我不明白Django Templates如何在Contexts中访问嵌套数据。我可以访问嵌套在上下文数据结构中的字典值。符号 - {{aDictionary.i_am_a_key}}运行正常。但是,如果我尝试迭代一个键列表并从同一个字典中获取它们的值,我什么也得不到。所以

{% for key in keys_list %}{{ aDictionary.key }}{% endfor}}

{%for key in keys_list%} {{aDictionary.key}} {%endfor}}

just generates blanks.

只是生成空白。

What am I missing here? Does Django not support key access to context dictionaries on the fly? Do I need to write a custom tag to do this?

我在这里想念的是什么? Django不支持动态上下文字典的密钥访问吗?我是否需要编写自定义标记才能执行此操作?

EDIT

编辑

My examples assume these data structures:

我的例子假设这些数据结构:

aDictionary = {'i_am_a_key': 'all good', 'i_am_another_key': 'okay'}
keys_list = ['i_am_a_key', 'i_am_another_key']

3 个解决方案

#1


5  

This is a fundamental limitation of the Django templating language.

这是Django模板语言的基本限制。

Three solutions:

三种解决方案

  1. Use {% for key,value in foo.items %} to get key and value.
  2. 使用{%for key,value in foo.items%}来获取键和值。
  3. Use Jinja2 -- an almost Django-like templating system.
  4. 使用Jinja2 - 一个几乎类似Django的模板系统。
  5. User the expr djangosnippet to do the access math.
  6. 用户使用expr djangosnippet进行访问数学运算。

#2


3  

This is a different approach, but based on what you want to accomplish this is the angle I'd take.

这是一种不同的方法,但基于你想要实现的目标,这就是我所采取的角度。

If you want to keep a subset of some dictionary and you want to iterate around it's values in some ordered fashion, I'd copy the element you're interested in into a SortedDict (django/utils/datastructures.py).

如果你想保留一些字典的子集,并且想要以某种有序的方式迭代它的值,我会将你感兴趣的元素复制到SortedDict(django / utils / datastructures.py)中。

In my mind, stuff like this should live in the view (all of this is untested):

在我看来,像这样的东西应该存在于视图中(所有这些都是未经测试的):

sorted_dict = SortedDict()
for key in orig_dict:
    if interested(key):
        sorted_dict[key] = orig_dict[val]

and the templates should just be very dumb:

模板应该是非常愚蠢的:

{% for key, val in sorted_dict.items %}{{ val }}{% endfor}}

#3


2  

It's not the same question, but the answer is similar to #844746.

这不是同一个问题,但答案类似于#844746。

You end up with a filter which you can do...

你最终得到了一个过滤器,你可以做...

{% load getattribute %}
{% for key in keys_list %}
    {{ aDictionary|attr:key }}
{% endfor %}

#1


5  

This is a fundamental limitation of the Django templating language.

这是Django模板语言的基本限制。

Three solutions:

三种解决方案

  1. Use {% for key,value in foo.items %} to get key and value.
  2. 使用{%for key,value in foo.items%}来获取键和值。
  3. Use Jinja2 -- an almost Django-like templating system.
  4. 使用Jinja2 - 一个几乎类似Django的模板系统。
  5. User the expr djangosnippet to do the access math.
  6. 用户使用expr djangosnippet进行访问数学运算。

#2


3  

This is a different approach, but based on what you want to accomplish this is the angle I'd take.

这是一种不同的方法,但基于你想要实现的目标,这就是我所采取的角度。

If you want to keep a subset of some dictionary and you want to iterate around it's values in some ordered fashion, I'd copy the element you're interested in into a SortedDict (django/utils/datastructures.py).

如果你想保留一些字典的子集,并且想要以某种有序的方式迭代它的值,我会将你感兴趣的元素复制到SortedDict(django / utils / datastructures.py)中。

In my mind, stuff like this should live in the view (all of this is untested):

在我看来,像这样的东西应该存在于视图中(所有这些都是未经测试的):

sorted_dict = SortedDict()
for key in orig_dict:
    if interested(key):
        sorted_dict[key] = orig_dict[val]

and the templates should just be very dumb:

模板应该是非常愚蠢的:

{% for key, val in sorted_dict.items %}{{ val }}{% endfor}}

#3


2  

It's not the same question, but the answer is similar to #844746.

这不是同一个问题,但答案类似于#844746。

You end up with a filter which you can do...

你最终得到了一个过滤器,你可以做...

{% load getattribute %}
{% for key in keys_list %}
    {{ aDictionary|attr:key }}
{% endfor %}