2011-09-26 85 views
5

我想要得到以下工作。用于循环变量作为Django模板中的字典键

计数循环需要遍历所有值,并且可能没有与每个计数相关联的用户,但需要在每个循环中使用计数值i传递给JavaScript。

蟒蛇部分:

users = {} 
users[1]={} 
users[1][id]=... 
users[1][email]=... 

... 

count=[1,2,3,4,5,6,7,8,9,10] 

Django的模板部分:

{% for i in count %} 
do some stuff with the value i using {{i}} which always returns the value, i.e. 1 

email:{% if users.i.email %}'{{users.i.email}}'{% else %}null{% endif%} 
{% endfor %} 

这不返回任何电子邮件。 当我替补人数1i{% if user.i.email %}电子邮件返回用户的电子邮件地址。 我在JavaScript中使用数据,因此如果它不存在,它需要隐式为空。 我似乎无法让Django将i变量识别为变量而不是值i。

使用[]不起作用,因为它抛出一个无效的语法错误

email:{% if users.[i].email %}'{{users.[i].email}}'{% else %}null{% endif%} 

我一直在使用 “with” 语句

{% for i in count %}{% with current_user=users.i %}... 

,然后使用current_user.email尝试,但不会返回任何

也试图

{% for i in count %}{% with j=i.value %}... 

以防万一它会工作,然后试图用j,但同样的结果。

我曾想过建立一个内部for循环遍历用户对象和检查,如果我是等于键/值,但似乎昂贵,不是很可扩展性。

任何想法我怎么能强迫Django的查看i作为一个变量和使用它的值作为索引,或任何其他方式解决这个问题?

感谢

Jayd

* 编辑:

我尝试了额外的循环,通过ABHI的建议,以下。

{% for i in count %} 
    {% for key, current_user in users.items %} 
     do some stuff with the value i using {{i}} which always returns the value, i.e. 1 
     email:{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%} 
    {% endfor %} 
{% endfor %} 

这种类型的作品,但现在它会重复do some stuff with the value i为用户的每一个值。如果我把一个if:

{% for i in count %} 
    {% for key, current_user in users.items %} 
    {% if i == key %} 
     do some stuff with the value i using {{i}} which always returns the value, i.e. 1 
     email:{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%} 
    {% endif%} 
    {% endfor %} 
{% endfor %} 

当计数没有一个特定的用户时,忽略循环。

我可以解决此看到的唯一方法是有那个我想用current_user每个地方的用户环路。

{% for i in count %} 
     do some stuff with the value i using {{i}} which always returns the value, i.e. 1 
     email:{% for key, current_user in users.items %}{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}{% endfor %} 
{% endfor %} 

而这看起来非常昂贵。 任何想法?

我在想,也许的写一个过滤器,使用i作为关键用户返回值:

{% with current_user=users|getuser:i %} 

但我不知道这是否会工作或我会得到同样的错误,在这里i作为值“i”而不是变量名传递。

我会试一试这么久。

* 编辑

这没有奏效。 过滤器使用{{}}返回对象,但它不在{% %} 内工作。

感谢输入

+1

你为什么要使用字典外集合,而不是一个列表? –

+0

为什么在整个问题中使用** bold **格式,而不是在编辑框右侧显示明确的格式提示? –

+0

格式化的道歉,我没有看到我发布帖子时的提示。 – Jayd

回答

1

我想出了以下的解决方法:

这是在滤波代码:

@register.filter(name='dict_value_or_null') 
def dict_value_or_null(dict, key): 
    if key in dict: 
     return dict[key] 
    else: 
     return 'null' 

这里是模板代码

{% for i in count %} 
     do some stuff with the value i using {{i}} which always returns the value, i.e. 1 
     email:'{{users|dict_value_or_null:i|dict_value_or_null:'email'}}' 
{% endfor %} 

这种运作良好,因此我想有我的解决方案。但是我仍然认为,如果模板系统中有一种方法可以将{%%}中的值强制作为变量而不是文字来处理,那么这一切都会变得更加容易。

有没有办法做到这一点?

感谢输入

+0

可能不是您正在寻找的答案,但您可以看看Jinja2的模板引擎。它相当好地插入到Django中,具有非常相似的语法,但设计得更好一些(恕我直言),尤其是当涉及到能够在模板中使用类似Python的构造时。 –

+0

谢谢。我会检查一下。 – Jayd

1

试试这个:

{% for key,val in user.items %} 
ID is {{ val.id }} and Email is {{ val.email }} 
{% endfor %} 
+0

注 - 您已经使用顶部字典“user”和用户[i]尝试访问索引i。确保在尝试访问用户时字典名称匹配。items – Abhi

+0

感谢您的回答。是的,这是我正在谈论的工作,在另一个循环内部有一个for循环。它已经在代码中工作了,但是有没有其他方式可以做到这一点,它不需要循环中的循环,而是通过i调用用户对象? – Jayd

+0

感谢您对用户和用户的评论。我回去改变它。 – Jayd

9

乔在他的评论说,一本字典显然是外部容器错误的数据结构。你应该使用一个列表。

users = [] 
user = {'id':..., 'email': ...} 
users.append(user) 

...

{% for user in users %} 
    {{ user.email }} 
{% endfor %} 

如果您需要在循环中的每个用户的数量,你可以使用{{ forloop.counter }}

+0

我需要从1开始计数,这就是为什么我使用单独的计数列表,并且还有一些计数将存在,没有用户与他们关联。所以可能只有2个用户,我可能需要循环并用计数6或8次来完成其他事情。 – Jayd

+0

@Jayd仍然不确定我是否完全理解了用例,但是这些细节本来很好,因此我们知道您想要解决的实际问题。 –

1

使Jayd代码可与任一列表或字典,尝试这种

@register.filter(name='dict_value_or_null') 
def dict_value_or_null(dict, key): 
    try: 
     return dict[key] 
    except: 
     return 'null'