0

I am trying to use for loop though a dictionary in django template and inside the for loop, I am trying to nest another for loop to loop through say quantity for displaying those many images of product -

the template is like this -

{% for product_id, item in b_data.items %}
    {% for i in item.numItems %}
        <div class="col-md-4 mb-4">
            <div class="card" style="width: 18rem;">
                <img src="/media/{{item.image}}" class="card-img-top" alt="...">
                <div class="card-body">
                <h5 class="card-title">{{item.title}}</h5>
                <p class="card-text">{{product_id}} {{item.qty}}</p>
                    <div class="card-footer">
                        <a href="#" class="btn btn-primary"><i class="bi bi-cart2"></i></a>
                    </div>
                </div>
            </div>
        </div>
    {% endfor %}
{% endfor %}

The views.py looks like below -

def make_your_box(request):
    box_p = {}
    box_p[str(request.GET['id'])]={
        'image':request.GET['image'],
        'title':request.GET['title'],
        'qty':request.GET['qty'],
        'price':request.GET['price'],
        'numItems': list(range(1, int(request.GET['qty'])+1)),
    }
    print(box_p)
    
    if 'boxdata' in request.session:
        if str(request.GET['id']) in request.session['boxdata']:
            box_data=request.session['boxdata']
            box_data[str(request.GET['id'])]['qty']=int(box_data[str(request.GET['id'])]['qty'])+1
            box_data[str(request.GET['id'])]['numItems']=list(range(1,int(box_data[str(request.GET['id'])]['qty'])+1)),
            box_data.update(box_data)
            request.session['boxdata']=box_data
        else:
            box_data=request.session['boxdata']
            box_data.update(box_p)
            request.session['boxdata']=box_data
    else:
        request.session['boxdata']=box_p
    print(request.session['boxdata'])
    print(len(request.session['boxdata']))
    x = 0    
    for prodid, item in request.session['boxdata'].items():
        x = x + int(item['qty'])
    print(x)    
    t_box=render_to_string('ajax/TestSelect1_1.html',{'b_data':request.session['boxdata']})
    return JsonResponse({'b_data':t_box})

even in the print stmts outputs correct outpput in the command prompt as shown below -

[06/Nov/2021 23:05:30] "GET /TestSelect1 HTTP/1.1" 200 13945
[06/Nov/2021 23:05:30] "GET /media/product_imgs/IMG_0910.JPG HTTP/1.1" 200 3462312
{'5': {'image': 'product_imgs/RedCookies.jpg', 'title': 'Strawberry Cookies', 'qty': '1', 'price': '10', 'numItems': [1]}}
{'5': {'image': 'product_imgs/RedCookies.jpg', 'title': 'Strawberry Cookies', 'qty': '1', 'price': '10', 'numItems': [1]}}
1
1
[06/Nov/2021 23:05:37] "GET /make_your_box?id=5&qty=1&title=Strawberry%20Cookies&price=10&image=product_imgs%2FRedCookies.jpg HTTP/1.1" 200 619
{'5': {'image': 'product_imgs/RedCookies.jpg', 'title': 'Strawberry Cookies', 'qty': '1', 'price': '10', 'numItems': [1]}}
{'5': {'image': 'product_imgs/RedCookies.jpg', 'title': 'Strawberry Cookies', 'qty': 2, 'price': '10', 'numItems': ([1, 2],)}}
1
2
[06/Nov/2021 23:05:49] "GET /make_your_box?id=5&qty=1&title=Strawberry%20Cookies&price=10&image=product_imgs%2FRedCookies.jpg HTTP/1.1" 200 619
{'6': {'image': 'product_imgs/IMG_0903.JPG', 'title': 'Decoupage', 'qty': '1', 'price': '20', 'numItems': [1]}}
{'5': {'image': 'product_imgs/RedCookies.jpg', 'title': 'Strawberry Cookies', 'qty': 2, 'price': '10', 'numItems': [[1, 2]]}, '6': {'image': 'product_imgs/IMG_0903.JPG', 'title': 'Decoupage', 'qty': '1', 'price': '20', 'numItems': [1]}}
2
3
[06/Nov/2021 23:06:51] "GET /make_your_box?id=6&qty=1&title=Decoupage&price=20&image=product_imgs%2FIMG_0903.JPG HTTP/1.1" 200 1213
{'6': {'image': 'product_imgs/IMG_0903.JPG', 'title': 'Decoupage', 'qty': '1', 'price': '20', 'numItems': [1]}}
{'5': {'image': 'product_imgs/RedCookies.jpg', 'title': 'Strawberry Cookies', 'qty': 2, 'price': '10', 'numItems': [[1, 2]]}, '6': {'image': 'product_imgs/IMG_0903.JPG', 'title': 'Decoupage', 'qty': 2, 'price': '20', 'numItems': ([1, 2],)}}
2
4
[06/Nov/2021 23:06:54] "GET /make_your_box?id=6&qty=1&title=Decoupage&price=20&image=product_imgs%2FIMG_0903.JPG HTTP/1.1" 200 1213

but the template doesn't show those many images displayed as expected, looks like its not looping through item.numItems mentioned in template pasted above - can any expert please have a look as explain what might be happening...

thanks a lot.

shoonya007
  • 23
  • 5

1 Answers1

0

Your inner for-loop format will cause the wrong result:

{% for i in >>>item.numItems<<< %}

You should change it to a range for-loop. like this:

{% for i in '0123456789'|make_list %}
    {{ i }}
{% endfor %}

Or like this:

{% for i in range(item.numItems) %}
    {{ i }}
{% endfor %}

Read More : using numeric for loop in a Django template

Hamid Rasti
  • 813
  • 1
  • 6
  • 16