2011-09-26 84 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'