开发者

python dictionaries and django

开发者 https://www.devze.com 2023-03-27 18:17 出处:网络
hi I have a python dictionary. How do I output the list of keys in django? If I do something like {% for key in d开发者_高级运维ict.keys() %}

hi I have a python dictionary. How do I output the list of keys in django?

If I do something like

{% for key in d开发者_高级运维ict.keys() %}

<tr>
  <td>
    key
  </td>

</tr>   

{% endfor %}

I get

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

Thanks!


You have to use {{ key }} inside the loop for this to work. Also:

{% for key in dict.keys %}


You just need dict.keys, not dict.keys() -- the Django template system will automatically try to call any part of the variable that's callable.

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


Don't call the dict.keys method in the for loop, and you'll need to actually print out the key var by doing {{ key }} in the forloop. Like this:

{% for key in dict.keys %}

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

</tr>   
{% endfor %}

See Accessing Method Calls in the django documentation.


You have to use {{ key }} instead of just key and dict.keys instead of dict.keys()

0

精彩评论

暂无评论...
验证码 换一张
取 消