-3

I want to show this piece of data in template table

{u'User1': {'annual_spend__sum': None}, u'User2': {'annual_spend__sum': 80}, u'User3': {'annual_spend__sum': 30}, u'User4': {'annual_spend__sum': None}}

I am not sure how to loop over the data to show it properly can anyone help?

this_user = User.objects.get(id=3)
    all_users = User.objects.all()
    values = {}
    for this_user in all_users:
        values[this_user.username] = Lead.objects.filter(assign_to=this_user).exclude(lead_status='CV').aggregate(Sum('annual_spend'))
    values.items()


    return render(request, 'index.html', {'annual_spend': values})

Updated

def func(request):
    from django.template import context
    this_user = User.objects.get(id=3)
    all_users = User.objects.all()

    values = {}
    for this_user in all_users:
        values[this_user.username] = Lead.objects.filter(assign_to=this_user).exclude(lead_status='CV').aggregate(Sum('annual_spend'))

    context = {}
    annual_spend__sum_list = []
    data = values

    for i in data:
        annual_spend__sum_list.append(data[i]['annual_spend__sum'])

    context['result'] = annual_spend__sum_list

    return render(request, "index.html", context)

Template

<ul>
{% for i in result %}
<li>{{ i }}</li>
{% endfor %}
</ul>
  • possible duplicate of [how to iterate through dictionary in a dictionary in django template?](http://stackoverflow.com/questions/8018973/how-to-iterate-through-dictionary-in-a-dictionary-in-django-template) – Sayse Aug 20 '15 at 12:52
  • Duplicate is just one of many links found on google by searching for "django iterate dict".... – Sayse Aug 20 '15 at 12:53
  • @Sayse I don't want to be rude, but that topic didn't answer my question. – ohhellothere Aug 20 '15 at 12:59
  • 1
    What is your question then? From what I can tell you are looking to iterate over a dictionary (`annual_spend`) in a template. what have you tried/researched? – Sayse Aug 20 '15 at 14:00
  • @Sayse Yes basically what I want to do is show the the data in the template so it looks something like this http://codepad.org/jkenMTTG – ohhellothere Aug 21 '15 at 09:21
  • That is what the duplicate does. – Sayse Aug 21 '15 at 10:16
  • @Sayse please elaborate – ohhellothere Aug 21 '15 at 10:50

1 Answers1

-1

Just iterate through it like any other dictionary.

data = {'User1': {'annual_spend__sum': None}, 'User2': {'annual_spend__sum': 80}, 'User3': {'annual_spend__sum': 30}, 'User4': {'annual_spend__sum': None}}

for i in data:
    print i, data[i], data[i]['annual_spend__sum']

This is the output:

>>> data = {'User1': {'annual_spend__sum': None}, 'User2': {'annual_spend__sum': 80}, 'User3': {'annual_spend__sum': 30}, 'User4': {'annual_spend__sum': None}}
>>> 
>>> for i in data:
...     print i, data[i], data[i]['annual_spend__sum']
... 
User4 {'annual_spend__sum': None} None
User2 {'annual_spend__sum': 80} 80
User3 {'annual_spend__sum': 30} 30
User1 {'annual_spend__sum': None} None
>>> 

Now, you can do whatever you want with it.

I suggest that you make a context dict and put a list of annual_spend__sum in it and then pass it to the render.

Do it like this:

def func(request):
    context = {}
    annual_spend__sum_list = []
    data = {'User1': {'annual_spend__sum': None}, 'User2': {'annual_spend__sum': 80}, 'User3': {'annual_spend__sum': 30}, 'User4': {'annual_spend__sum': None}}

    for i in data:
        annual_spend__sum_list.append(data[i]['annual_spend__sum'])

    context['result'] = annual_spend__sum_list

    return render(request, "index.html", context)

Now, in index.html you can print like this:

<ul>
    {% for i in result %}
        <li>{{ i }}</li>
    {% endfor %}
</ul>

You can do it in a lot of different ways. This was the most basic and easiest to explain. See in for loop, I directly referred to the key and iterated through the results.

Abhyudit Jain
  • 3,640
  • 2
  • 24
  • 32