Andrew McNoodles
Andrew McNoodles

Reputation: 31

Django Template - Nested Dictionary Iteration

My template receives from views.py following nested dictionary of shopping cart content.

{
'20': 
    {'userid': 1, 
    'product_id': 20, 
    'name': 'Venus de Milos', 
    'quantity': 1, 
    'price': '1500.00', 
    'image': '/media/static/photos/pngegg.png'}, 
'23': 
    {'userid': 1, 
    'product_id': 23, 
    'name': 'Bicycle', 
    'quantity': 1, 
    'price': '1000.00', 
    'image': '/media/static/photos/366f.png'}
}

I am having problem with iteration through it. For example, when I am using following code,

{% for key, value in list %}

{{ key }} {{ value }}

{% endfor %}

instead of keys and values I receive just this:

2 0 
2 3

My goal is to calculate grand total through multiplying quantity and price for each product and dding it all together with each product in cart.

May sombody give me a hand on this, or at least help to figure out how to iterate properly through nested dictionary?

i am using following lib for cart: https://pypi.org/project/django-shopping-cart/

views.py:

@login_required(login_url="/users/login")
def cart_detail(request):
    cart = Cart(request)
    queryset = cart.cart
    context = {"list": queryset }
    return render(request, 'cart_detail.html', context)

SOLVED (kind of): Following your advice, I've wrote calculation for "total" in views.py BUT, since dictionary of product has 6 attributes, "total" is added 6 times in loop, for each product in cart. For now I've just added division by 6, but obviously this is not rational solution

def cart_detail(request):
    cart = Cart(request)
    queryset = cart.cart

    total_price=0

    for key, value in queryset.items():
        for key1, value1 in value.items():
            total_price = total_price + (float(value['quantity']) * float(value['price']))
    #Temporal decision
    total_price = total_price / 6
    
    context = {"list": queryset, "total_price": total_price }
    return render(request, 'cart_detail.html', context)

Upvotes: 2

Views: 163

Answers (2)

Tms91
Tms91

Reputation: 4164

I suggest you to do the calculations in views.py, save them into variables and then pass it to template.

Assuming that your

is saved in the variable cart_dict:

    total_price=0

    for product in cart_dict:            
        total_price = total_price + (float(product['quantity']) * float(product['price']))
    
    context = {"cart_dict: cart_dict, "total_price": total_price }
    return render(request, 'cart_detail.html', context)
    

Upvotes: 1

Carlos
Carlos

Reputation: 472

you can try like this:

{% for key, value in list.items %} <-first loop
   {{ key }}
   {% for key1, value1 in value.items %} <-- second loop
      {{ key1 }} - {{ value1 }}
   {% endfor %}
{% endfor %}

{{ key }} will give you the key of outer dict, in your case 20 and 23
{{ key1 }} will give you the key of nested dict user_id, name,...
{{ value1 }} will give you the value of nested dict.

Hope it can help

Upvotes: 2

Related Questions