2011-08-10 45 views
0

您好我有一個python字典。如何輸出django中的密鑰列表?python dictionaries和django

如果我這樣做

{% for key in dict.keys() %}

<tr> 
    <td> 
    key 
    </td> 

</tr> 

{% endfor %}

我得到

TemplateSyntaxError: Could not parse the remainder: '()' from 'dict.keys()'

謝謝!

回答

1

您必須在循環中使用{{key}}才能使其工作。另外:

{%爲dict.keys%鍵}

0

不要調用dict.keys方法在for循環,你會需要實際打印出來的關鍵變種通過在for循環做{{ key }} 。像這樣:

{% for key in dict.keys %} 

<tr> 
    <td> 
    {{ key }} 
    </td> 

</tr> 
{% endfor %} 

請參閱django文檔中的Accessing Method Calls

0

你必須使用{{ key }},而不是隻是keydict.keys代替dict.keys()

1

你只需要dict.keys,不dict.keys() - Django模板系統將自動嘗試撥打這就是調用該變量的任何部分。

{% for key in list.keys %} 
<tr><td>{{ key }}</td></tr> 
{% endfor %}