-1

In my views.py i have :

credits_total = Course.objects.filter(dept = dept_code1, b_tech = 1, type = 1).values('sem').annotate(sum = Sum('credits'))

So credits_total has the following:

credits_total = [{'sem': 3, 'sum': 19},{'sem': 4, 'sum': 20},{'sem': 5, 'sum': 21},{'sem':6,'sum':22},{'sem':7,'sum':21},{'sem':8,'sum':24}]

I want to do somthing like:

{% for i in "012345" %}
    ....
    <b> {{credits_total[i]['sum']}} </b>
    ...
{% endfor %}

How can i access a specific key in the dictionary at a specific index in the list?

I'm new to django so could you Plz explain in detail. Thank you!

Gocht
  • 9,924
  • 3
  • 42
  • 81
sonu.Shiva
  • 23
  • 3
  • 10

3 Answers3

1

Python dictionaries are best thought of as unordered sets. You shouldn't try to index them... That said, you can iterate over your dictionary data and print it as follows.

{% if credits_total %}
    {% for item in credits_total %}
        sem:{{ item.sem }}, sum:{{ item.sum }}
    {% endfor %}
{% endif %}
Community
  • 1
  • 1
pedwards
  • 423
  • 2
  • 12
  • even this doesn't seem to give me the result. It doesn't give any value! – sonu.Shiva Jun 03 '15 at 20:53
  • Are you seeing anything in credits_total at all? maybe you are not passing the data correctly. This works fine for me with your exact data. – pedwards Jun 03 '15 at 21:12
  • to be exact i'm doing something like this in views.py: `context_dict['credits_total'] = credits_total` and `btech_list = zip(collapse_num,sem_title_btech,theory_btech,practical_btech,integers) ` where `integers = [0,1,2,3,4,5]` and` context_dict['btech_list'] = btech_list `and ` return render (request,'course.html', context_dict) ` then in django template `{% for i,j,k,l,m in btech_list %} `.....` {{credits_total.m.sum}}`. But this doesn't give any data at all. It gives the results when i do `{{credits_total.1.sum}}` or with a direct input of any other number. – sonu.Shiva Jun 03 '15 at 23:07
  • Why are you trying to dump the `credits_total` dictionary by iterating through `btech_list`? Simplify... remove `btech_list` from the equation. `credits_total` is a different object entirely and should be handled separately. – pedwards Jun 04 '15 at 12:54
0

In template, you can use dot notation. Like this:

<b>{{credits_total.i.sum}}</b>

But the problem with this is that i var inside the loop is not a number. You need to write your own tag filter to get something like a regular range() in python. See this: create range filter

Gocht
  • 9,924
  • 3
  • 42
  • 81
  • i tried it but the data doesn't show up neither does it throw any error!! – sonu.Shiva Jun 03 '15 at 20:05
  • i tried to hard code it by replacing i with 0. it still doesn't work! – sonu.Shiva Jun 03 '15 at 20:46
  • I tried this using numbers instead of i, and it worked for me. `{{credits_total.1.sum}}` – Gocht Jun 03 '15 at 20:50
  • to be exact i'm doing something like this in views.py: `context_dict['credits_total'] = credits_total` and `btech_list = zip(collapse_num,sem_title_btech,theory_btech,practical_btech,integers) ` where `integers = [0,1,2,3,4,5]` and` context_dict['btech_list'] = btech_list `and ` return render (request,'course.html', context_dict) ` then in django template `{% for i,j,k,l,m in btech_list %} `.....` {{credits_total.m.sum}}`. But this doesn't give any data at all. It gives the results when i do `{{credits_total.1.sum}}`. Now m is a set of int values, but still it doesn't give the data! – sonu.Shiva Jun 03 '15 at 23:11
0

This did the trick: In views.py:

    from itertools import izip_longest
    def course(request, dept_code):
    ....
    sum_credits_total = list()
    ....
    for i in credits_total: sum_credits_total.append(i['sum'])
    ....
    total_list  = izip_longest(sum_credits_total,hours_total,sum_sem_credits,sum_sem_hours)
    context_dict['total_list'] = total_list
    return render (request,'course.html', context_dict)

And in template:

    {% for i,j,k,l in total_list %}
         {{i}}....{{j}}....{{k}}....{{l}}
    {% endfor %}
sonu.Shiva
  • 23
  • 3
  • 10