-4

I want to know how yu can incorporate it into the html. What I have right now is the following.

{% for u in list %}
    {{ u }} <br>
{% endfor %}

And what I want is something along these lines-

{% for u in list %}
    {{ u }} <br>
    {{ u.value }} <br>
{% endfor %}

Thanks guys!

EDIT: A sample would look like the following-

Apple
10
---
Banana
4
---
Orange
3
---
Pear
1
---
Ravaal
  • 3,233
  • 6
  • 39
  • 66
  • 2
    How are each of them outputting for you that isn't meeting your expectation, and what is your expectation on how it should look like? Can you provide a sample please. – idjaw Jun 12 '17 at 00:00
  • I added an example. @Chris, I knew that I might've been right. I wasn't – Ravaal Jun 12 '17 at 00:06
  • 1
    "I knew that I might've been right"—then why didn't you just _try it_?! It would have been much faster than typing out a question on Stack Overflow! – ChrisGPT was on strike Jun 12 '17 at 00:07
  • I tried it and it didn't work – Ravaal Jun 12 '17 at 00:08
  • 1
    @NickTheInventor What does "it didn't work" mean in this case? Providing these details is imperative for the readers to know what is going on to know how to help you. How do the outputs look like based on the code you provided, and how do they look like that indicates to you that it is problematic? Furthermore, dictionary iteration should be done as `for k, v in your_dict.items()` – idjaw Jun 12 '17 at 00:10
  • 1
    Please read [ask]. So far you aren't asking your question very effectively. – ChrisGPT was on strike Jun 12 '17 at 00:12
  • Furthermore, in particular for Django, this apparently *should* be the way dictionary iteration occurs while keeping in line with the templating syntax rules: https://stackoverflow.com/a/6285769/1832539. – idjaw Jun 12 '17 at 00:17

1 Answers1

3

Here you go.

{% for key, value in list.items %}
    {{ key }} <br>
    {{ value }} <br>
    --- <br>
{% endfor %}

You can access nested items this way as well. In your view:

grocery_list = {
    'Apples': {'Red Disgusting': 5, 'Granny Smith': 3}, 
    'Snack Food': {'Spam': 4, 'Eggs': 1}
}

In your template:

{% for food_type, list in grocery_list.items %}
    <h3>{{ food_type }}</h3
    {% for item, quantity in list.items %}
        <p>{{ item }}: {{ quantity }}</p>
    {% endfor %}
{% endfor %}

Result:

Apples

Red Disgusting: 5

Granny Smith: 3

Snack Food

Spam: 4

Eggs: 1

DragonBobZ
  • 2,194
  • 18
  • 31