Django模板在使用for循环和index时不渲染数据

gkn4icbw  于 2023-05-19  发布在  Go
关注(0)|答案(1)|浏览(142)

我在Django中实现了一个功能,它在一个单独的细节页面上显示订单细节
我还创建了一个自定义标签,它返回给定列表的长度范围(这工作正常):
下面是我的模板:

{% 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>
...

我面临的问题是循环{% for i in order.products}下的数据|get_range %}没有被渲染我用数字0,1,2替换了i(因为列表的长度为3),以检查问题是否在上下文字典中,但它不是渲染数据的情况,而是当我使用i时。
下面是从veiw传递到template的上下文字典:

{'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}}

我试图硬编码的索引,它是工作,但不是与索引变量可以有人PLS给予这个问题的任何解决方案

xxslljrj

xxslljrj1#

看起来问题出在您尝试使用索引i访问order.products列表的方式上。在模板中,不能使用变量动态访问对象的属性

相关问题