简体   繁体   中英

python dictionaries and django

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

If I do something like

{% for key in dict.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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM