Django Template - Nested Dictionary Iteration

Andrew McNoodles

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)
Tms91

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)
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django template in nested dictionary

From Dev

Django template in nested dictionary

From Dev

Nested dictionary - iteration off

From Dev

Django grouping dictionary in a template

From Dev

Iterate dictionary in django template

From Dev

Access dictionary in Django template

From Dev

Django dynamic nested template

From Dev

Django: nested template commands

From Dev

Django template nested for loop

From Dev

How do I access only first element in nested dictionary in Django template?

From Dev

Django template skip line every two iteration

From Dev

Django template skip line every two iteration

From Dev

Dictionary Index of dictionary contained in a list in Django template

From Dev

Show the dictionary key in django template

From Dev

Accessing dictionary by key in Django template

From Dev

Accessing dictionary values in django template

From Dev

Dictionary in Django template - filter usage

From Dev

django template view nested list

From Dev

Django celery task: dictionary changed size during iteration

From Dev

Django celery task: dictionary changed size during iteration

From Dev

Django Template Tag Loop dictionary variable

From Dev

django template: get data from dictionary

From Dev

How to combine a QuerySet and a Dictionary in Django Template?

From Dev

How to use function returnig dictionary in django template?

From Dev

Get value from a dictionary key in django template

From Dev

Django: multi-dimensional dictionary displayed in template

From Dev

How to iterate through a dictionary with lists in django template?

From Dev

Django - dictionary key and value not displaying in template

From Dev

processing through a dictionary of lists in a Django template

Related Related

HotTag

Archive