1

I've reading all the template tags posts regarding the loop variable in the key. Apparently Django does not support loop variable in key and I am not sure how to use the custom template tag.

I wanted to display something like this, but how can I achieve this with {% for i in mData %} loop ?

{{ mData.0.name }}
{{ mData.1.name }}
{{ mData.2.name }}

{{ mData.0.age }}
{{ mData.1.age }}
{{ mData.2.age }}

mData is a list of dictionaries.

mData = { "name":"alex", "age":"12"},{"name":"amy","age":"14"} ...

chad
  • 627
  • 1
  • 8
  • 24
  • 1
    What is the key in this case? Because it looks like `mData` is a list of dictionaries. – dhke Aug 31 '15 at 07:30
  • yes it's a list of dictionaries... any idea how can I loop through that ? I tried {% for loop in mData.forloop.counter.name %} but it wouldn't work... – chad Aug 31 '15 at 07:41
  • In this case, re-order the data structure in your view so that the dict passed to the template is `{'age': [age0, age1, ...], 'name': [name0, name1, ...]}` and so on. Otherwise you are putting data preparation code in your template. – dhke Aug 31 '15 at 07:48
  • thanks @dhke, but my data is over 5k entries.. it will be a pain to restruct them :) – chad Aug 31 '15 at 07:59

4 Answers4

2

Considering your data is in a list of dictionaries such as:

my_data = [{"name" : "abc" , "age":20,...}, {} , {}...]

You can access all attributes of each dictionary in your template this way:

{% for dict in my_data %}
<!-- Here dict would be each of the dictionary in my_data -->
<!-- You can access elements of dict using dot syntax, dict.property -->
   {{ dict.name }}, {{ dict.age }}, ... {{ dict.property }}
{% endfor %}

Reference links: Django templating language

If you want to structure your elements in the order you specifed, you can do something like this:

Name List:
{% for dict in my_data %}
my_data.name
{% endfor %}

Age List:
{% for dict in my_data %}
my_data.age
{% endfor %}

...
Prpoerty List:
{% for dict in my_data %}
my_data.property
{% endfor %}
aliasav
  • 3,048
  • 4
  • 25
  • 30
  • Thank you! This actually works as intended !!! Can't believe the solution is so simple! I was playing with custom template tag and reading about jinja etc etc. was about to give up!! – chad Aug 31 '15 at 07:58
  • 1
    Just go through the templating language chapter in the django docs, it well written and explained with a lot of examples. Once you go through them, you'll get a hang of it. http://www.djangobook.com/en/2.0/chapter04.html – aliasav Aug 31 '15 at 08:00
  • Thanks! I was actually reading it but couldn't quite figure it out yet and was confused with the template tag that I read on SO .. Thank you for the detailed answer too! One last question, what if I want to loop through all the properties ? Can I do that with a list, properties = ['name','age', ...] ? {% for i in properties %} my_data.properties {% endfor %} – chad Aug 31 '15 at 08:09
  • 1
    Yes, you may pass the properties in a list and iterate over them using the for template tag, also refer to this: http://stackoverflow.com/questions/2170228/iterate-over-model-instance-field-names-and-values-in-template – aliasav Aug 31 '15 at 08:23
1

Django template tags are intentionally very lightweight so that you don't put too much code into the template itself. If you need to do something complicated like loop over every other entry in the database, you should be setting that up in views.py instead of the template.

For the scenario you described, all you need to do is loop over the list of objects:

{% for data in datas %}
    {{ data.name }}
{% endfor %}
{% for data in datas %}
    {{ data.age }}
{% endfor %}
TheGRS
  • 103
  • 8
  • This wouldn't work... I need to access the list of dictionaries. for eg. mData.0.name ..... and I need to loop for 0 to 1000 and then name,age,sex... etc. Any idea? – chad Aug 31 '15 at 07:43
  • 1
    WIthin views.py you can set the filter. mData.objects.all()[:1000] should get the first 1000 entries in the database. – TheGRS Aug 31 '15 at 07:59
1

This solved the problem for me

{% for d in mData %}
{{ d.name }} {{ d.age }}
{% endfor %}
a134man
  • 150
  • 1
  • 9
  • Thanks! This actually works as intended !!! Can't believe the solution is so simple! I was playing with custom template tag and reading about jinja etc etc. was about to give up!! – chad Aug 31 '15 at 07:57
  • 1
    if this works, you can mark the answer as answered and please upvote – a134man Aug 31 '15 at 08:30
-1
{% for k, v in mData.items %}
    {{ k }} {{ v }}

by the way, PEP8 suggest we name the variable as lower + _, but not hump like javascript or other languages.

Sinux
  • 1,728
  • 3
  • 15
  • 28
  • Hmm this would not work since I need to access list of dictionaries ... mData.0.items instead of mData.items – chad Aug 31 '15 at 07:39