Title: Django Template not rendering data using when using for loop with index
Tags: python,django,django-views,django-templates

Question: I am implementing a functionality in Django which shows order details on a separate detail page
I have also created a custom tag which return the range of length of the list given to it(Which is working fine):
Here is my template :
{% extends 'Verification/main.html' %}
{% load custom_tags %}
{% block content %}
...
    <table class="table">
        {% for i in order.products|get_range %}
        {{i}}
        <tr>
            <td>{{order.products.i.productId}}</td>
            <td>
                <tr>
                    <td>Prodcut Name</td>
                    <td>{{order.products_info.i.name}}</td>
                </tr>
                <tr>
                    <td>Prduct SKU</td>
                    <td>{{order.products_info.i.sku}}</td>
                </tr>
                <tr>
                    <td>Prodcut Image</td>
                    <tr>
                        {% for image in order.products_info.i.images %}
                        <td><a src="{{image}}" download><img src="{{image}}"></a></td>
                        {% endfor %}
                    </tr>
                </tr>
                <tr>
                    <td>Prodcut Name</td>
                    <td>{{order.products_info.i.name}}</td>
                </tr>
            </td>
        </tr>
        {% endfor %}
    </table>
...

The problem I am facing is that data under the loop {% for i in order.products|get_range %} is not being rendered I replaced i with numbers 0,1,2 (as the list has length 3) to check if the problem is in the context dictionary but its not the case it is rendering data but not when I use i.
Here is the context dictionary which is passed to template from veiw:
{'order': {'customer': {'address': 'C-206, Block 14, Gulistan-e-johar, Karachi',
                        'email': 'maazkid2001@gmail.com',               
                        'id': ObjectId('6456af1e20657087f397d4ed'),     
                        'name': 'Maaz Ali',
                        'orders': [ObjectId('6456af7320657087f397d4fc'),
                                   ObjectId('6458c8ffb21581cef9d2d6e0')],
                        'password': 'maazali123',
                        'phone': '03172826520'},
           'customerId': '6456af1e20657087f397d4ed',
           'id': ObjectId('6456af7320657087f397d4fc'),
           'orderCreated': datetime.datetime(2023, 5, 7, 0, 50, 11),
           'products': [{'productId': ObjectId('644dfd74fb8cb045e300c7b6'),
                         'subtotal': 5000,
                         'units': '1',
                         'vendorId': '6441a8e7dfa8fbe1f953032f'},
                        {'productId': ObjectId('644ec14c3dfc15c3c19ad73f'),
                         'subtotal': 1999,
                         'units': '1',
                         'vendorId': '6441a8e7dfa8fbe1f953032f'},
                        {'productId': ObjectId('644d93c8dc222f4f33e84156'),
                         'subtotal': 500,
                         'units': '1',
                         'varId': '04ab0553-eaf6-46a3-afbc-0c5e59f211a5',
                         'vendorId': '644d71366913d75327c46ff6'}],
'products_info': [{'CreatedDateTime': datetime.datetime(2023, 4, 30, 10, 32, 32, 315000),
                              'category': '/Health & Beauty/Fragrances/Men '
                                          'Fragrances',
                              'condition': 'new',
                              'description': 'Product material Attar for men '
                                             'branded Features Black & Silver '
                                             'is Real Fragrance Discovered By '
                                             'Al Musk Long Lasting Fragrance '
                                             'Keep Original Pure Oil 12 ml is '
                                             'better than 100 ml Benefits '
                                             'Black And Silver By Al Mushk is '
                                             'a refined and sophisticated '
                                             'French fragrance, Introducing '
                                             'tangy aromas of the summer, '
                                             'Black And Silver by composition '
                                             'offers a refreshing choice for '
                                             'men with a penchant for the '
                                             'great outdoors.',
                              'height': '10',
                              'id': ObjectId('644dfd74fb8cb045e300c7b6'),
                              'images': ['https://ebazar-bucket.s3.amazonaws.com/dccb32fc-d52e-4ff6-9808-ba76712778bfmuskmahal1.jpeg',
                                         'https://ebazar-bucket.s3.amazonaws.com/4b4de65e-b046-4294-8118-816e0c461d8amuskmahal2.jpeg'],
                              'isVariation': 'no',
                              'isb2b': 'no',
                              'length': '20',
                              'manufacturer': 'Musk al mahal',
                              'name': 'Black And Silver By Musk Al Mahal | '
                                      'Original Attar For Men - 12ml',
                              'points': ['Black & Silver is Real Fragrance '
                                         'Discovered By Al Musk Long Lasting '
                                         'Fragrance Keep Original Pure Oil 12 '
                                         'ml is better than 100 ml',
                                         '',
                                         '',
                                         '',
                                         ''],
                              'price': '5000',
                              'reviews': {'count': {'length': 0, 'rate': 0},
                                          'reviewDetail': {}},
                              'sku': 'BAS-MuAlMah-12',
                              'status': 'enabled',
                              'units': 19,
                              'vendor': {'cnic': 4210153059423,
                                         'email': 'maazkid2001@gmail.com',
                                         'id': ObjectId('6441a8e7dfa8fbe1f953032f'),
                                         'name': {'_id': ObjectId('6441a8e7dfa8fbe1f953032f'),
                                                  'address1': 'C-206',
                                                  'address2': 'Block 10, '
                                                              'Gulistan-e-Johar',
                                                  'bankStatement': 'https://ebazarstorageserver.blob.core.windows.net/imageserver/1ca53e27-023d-44b7-bcc
2-c2af80ad7aa0Bank_Statement.png',
                                                  'billingAddress': 'C-206, '
                                                                    'Block 14, '
                                                                    'Gulistan-e-Johar, '
                                                                    'Karachi',
                                                  'businessType': 'individual',
                                                  'cardHolder': 'Maaz Ali',
                                                  'city': 'Karachi',
                                                  'cnic': 4210153059423,
                                                  'cnicBack': 'https://ebazarstorageserver.blob.core.windows.net/imageserver/2701ba14-a365-41db-a21c-03c
a42137c44cnicback.jpg',
                                                  'cnicFront': 'https://ebazarstorageserver.blob.core.windows.net/imageserver/671cbb05-8b27-4300-ae59-55
a581e25b11cnicfront.jpg',
                                                  'creditCard': '3423454345654536',
                                                  'dto': '2001-11-16',
                                                  'firstName': 'Maaz',
                                                  'isManufacturer': 'false',
                                                  'lastName': 'Ali',
                                                  'phoneNo': 3172826520,
                                                  'postalCode': '75290',
                                                  'province': 'Sindh',
                                                  'storename': 'Mega '
                                                               'Electronics'},
                                         'password': 'maazali123',
                                         'phone': '03172826520',
                                         'status': 'verified'},
                              'vendorId': '6441a8e7dfa8fbe1f953032f',
                              'weight': '500',
                              'width': '20'},
                             {'CreatedDateTime': datetime.datetime(2023, 5, 1, 0, 28, 9, 943000),
                              'category': '/Electronic Accessories/Computer '
                                          'Components/Processors',
                              'condition': 'new',
                              'description': 'The Arduino Leonardo is a '
                                             'micro-controller board base on '
                                             'the ATmega32u4 (datasheet). It '
                                             'has 20 digital input/output pins '
                                             '(of which 7 can be used as PWM '
                                             'outputs and 12 as analog '
                                             'inputs); a 16 MHz crystal '
                                             'oscillator, a micro USB '
                                             'connection, a power jack, an '
                                             'ICSP header, and a reset button. '
                                             'It contains everything need to '
                                             'support the microcontroller; '
                                             'simply connect it to a computer '
                                             'with a USB cable or power it '
                                             'with an AC-to-DC adapter or '
                                             'battery to get started.',
                              'height': '4',
                              'id': ObjectId('644ec14c3dfc15c3c19ad73f'),
                              'images': ['https://ebazar-bucket.s3.amazonaws.com/b64d6bf3-a808-4404-b9f8-db792c734309cca5dca3b14e56c2d4ecde92e9a9f83d.jp
g_720x720.jpg'],
                              'isVariation': 'no',
                              'isb2b': 'no',
                              'length': '10',
                              'manufacturer': 'none',
                              'name': 'Arduino Leonardo R3',
                              'points': ['', '', '', '', ''],
                              'price': '1999',
                              'reviews': {'count': {'length': 0, 'rate': 0},
                                          'reviewDetail': {}},
                              'sku': 'Aurduino-leo-101',
                              'status': 'enabled',
                              'units': 9,
                              'vendor': {'cnic': 4210153059423,
                                         'email': 'maazkid2001@gmail.com',
                                         'id': ObjectId('6441a8e7dfa8fbe1f953032f'),
                                         'name': {'_id': ObjectId('6441a8e7dfa8fbe1f953032f'),
                                                  'address1': 'C-206',
                                                  'address2': 'Block 10, '
                                                              'Gulistan-e-Johar',
                                                  'bankStatement': 'https://ebazarstorageserver.blob.core.windows.net/imageserver/1ca53e27-023d-44b7-bcc
2-c2af80ad7aa0Bank_Statement.png',
                                                  'billingAddress': 'C-206, '
                                                                    'Block 14, '
                                                                    'Gulistan-e-Johar, '
                                                                    'Karachi',
                                                  'businessType': 'individual',
                                                  'cardHolder': 'Maaz Ali',
                                                  'city': 'Karachi',
                                                  'cnic': 4210153059423,
                                                  'cnicBack': 'https://ebazarstorageserver.blob.core.windows.net/imageserver/2701ba14-a365-41db-a21c-03c
a42137c44cnicback.jpg',
                                                  'cnicFront': 'https://ebazarstorageserver.blob.core.windows.net/imageserver/671cbb05-8b27-4300-ae59-55
a581e25b11cnicfront.jpg',
                                                  'creditCard': '3423454345654536',
                                                  'dto': '2001-11-16',
                                                  'firstName': 'Maaz',
                                                  'isManufacturer': 'false',
                                                  'lastName': 'Ali',
                                                  'phoneNo': 3172826520,
                                                  'postalCode': '75290',
                                                  'province': 'Sindh',
                                                  'storename': 'Mega '
                                                               'Electronics'},
                                         'password': 'maazali123',
                                         'phone': '03172826520',
                                         'status': 'verified'},
                              'vendorId': '6441a8e7dfa8fbe1f953032f',
                              'weight': '0.4',
                              'width': '5'},
                             {'CreatedDateTime': datetime.datetime(2023, 4, 30, 3, 1, 43, 269000),
                              'brand': 'None',
                              'category': "/Men's Fashion/Shorts, Joggers & "
                                          'Sweats/Shorts',
                              'description': 'Product details of Pack of 2 '
                                             "Men's Denim Shorts Summer "
                                             'Fashion Casual Versatile Slim '
                                             'Fit Elastic Jeans Shorts for Men',
                              'height': '4',
                              'id': ObjectId('644d93c8dc222f4f33e84156'),
                              'images': ['https://ebazar-bucket.s3.amazonaws.com/b88c72e9-9416-416f-9216-2ccc2e48b7d7denimsshorts.PNG'],
                              'isVariation': 'yes',
                              'isb2b': 'no',
                              'length': '30',
                              'manufacturer': 'none',
                              'name': "Pack of 2 Men's Denim Shorts Summer "
                                      'Fashion Casual Versatile Slim Fit '
                                      'Elastic Jeans Shorts for_Men',
                              'points': ['Soft & Comfortable',
                                         'Jeans short',
                                         'Stretchable Denim',
                                         'Denim-Mix cotton',
                                         'Colors As shown in picture'],
                              'reviews': {'count': {'lenght': 0, 'rate': 0},
                                          'reviewDetail': {}},
                              'status': 'enabled',
                              'var_type': {'color': ['Blue', 'Black'],
                                           'size': ['S', 'M', 'L']},
                              'variations': {'04ab0553-eaf6-46a3-afbc-0c5e59f211a5': {'color': 'Blue',
                                                                                      'condition': 'new',
                                                                                      'price': '500',
                                                                                      'size': 'L',
                                                                                      'sku': 'short-dnm-L-Blu',
                                                                                      'units': 19},
                                             '11152e0e-1280-4b43-8adb-7b1fb91a7f05': {'color': 'Black',
                                                                                      'condition': 'new',
                                                                                      'price': '500',
                                                                                      'size': 'L',
                                                                                      'sku': 'short-dnm-L-Blk',
                                                                                      'units': 18},
                                             '40edb3ad-0652-4f94-a40b-37c40bc0c139': {'color': 'Black',
                                                                                      'condition': 'new',
                                                                                      'price': '500',
                                                                                      'size': 'M',
                                                                                      'sku': 'short-dnm-M-Blk',
                                                                                      'units': 21},
                                             '6e1efa32-173d-45ea-bafa-83420128d54c': {'color': 'Blue',
                                                                                      'condition': 'new',
                                                                                      'price': '500',
                                                                                      'size': 'M',
                                                                                      'sku': 'short-dnm-M-Blu',
                                                                                      'units': '20'},
                                             'c0cdb225-1c87-4c3d-96e7-b0a6368871bd': {'color': 'Blue',
                                                                                      'condition': 'new',
                                                                                      'mainpage': True,
                                                                                      'price': '500',
                                                                                      'size': 'S',
                                                                                      'sku': 'short-dnm-S-Blu',
                                                                                      'units': '20'},
                                             'cf30b8d6-c148-490f-a49c-45bcbe8d93e1': {'color': 'Black',
                                                                                      'condition': 'new',
                                                                                      'price': '500',
                                                                                      'size': 'S',
                                                                                      'sku': 'short-dnm-S-Blk',
                                                                                      'units': '20'}},
                              'vendor': {'cnic': 4220160352363,
                                         'email': 'saimrao2408@gmail.com',
                                         'id': ObjectId('644d71366913d75327c46ff6'),
                                         'name': {'_id': ObjectId('644d71366913d75327c46ff6'),
                                                  'addDetail': '32 Tina '
                                                               'square, Model '
                                                               'colony, '
                                                               'Karachi',
                                                  'address1': '32 Tina square',
                                                  'address2': 'Malir',
                                                  'bankStatement': 'https://ebazar-bucket.s3.amazonaws.com/68c23dfa-b72a-4b3b-af3a-ad204cfe8e0eWhatsAppI
mage2023-04-26at4.19.56AM(1).jpeg',
                                                  'billingAddress': '32 Tina '
                                                                    'square, '
                                                                    'Model '
                                                                    'colony, '
                                                                    'Karachi',
                                                  'businessType': 'individual',
                                                  'cardHolder': 'Saim Mehmood '
                                                                'Rao',
                                                  'city': 'Karachi',
                                                  'cnic': 4220160352363,
                                                  'cnicBack': 'https://ebazar-bucket.s3.amazonaws.com/a260ad54-5131-4aae-bce3-7f9dbf08d442WhatsAppImage2
023-04-26at4.19.56AM(1).jpeg',
                                                  'cnicFront': 'https://ebazar-bucket.s3.amazonaws.com/5bba22a4-bc45-4a55-96e6-409812aff521WhatsAppImage
2023-04-26at4.19.57AM(1).jpeg',
                                                  'creditCard': '5590490213451378',
                                                  'dto': '2001-08-24',
                                                  'firstName': 'Muhhamad',
                                                  'isManufacturer': 'false',
                                                  'lastName': 'Rao',
                                                  'middleName': 'Hassan ',
                                                  'phoneNo': 3002275960,
                                                  'postalCode': '75100',
                                                  'province': 'Sindh',
                                                  'storename': 'hassanclothing'},
                                         'password': 'saim123',
                                         'phone': '03002275960',
                                         'status': 'verified'},
                              'vendorId': '644d71366913d75327c46ff6',
                              'weight': '0.5',
                              'width': '25'}],
           'status': 'pending',
           'totalAmount': 7499}}



I tried to hard code the index it is working but not with the index variable can someone pls give any solution to this problem

Answer: It seems like the issue is with the way you are trying to access the order.products list using the index i. In the templates, you cannot use variables to dynamically access attributes of an object