django elasticsearch drf json до html шаблон - PullRequest
0 голосов
/ 20 июня 2020

Я использую функционал django_elasticsearch_dsl_drf, предлагающий искать из elasticsearch. Я пытаюсь разобрать вывод json из api в шаблон HTML. Однако я не уверен, как выполнить итерацию json, чтобы диктовать или перечислить, а затем использовать его в шаблоне HTML. Ниже приведен мой вывод функционального предложения api

{
"description_match": [
    {
        "text": "emot confe",
        "options": [
            {
                "_index": "products",
                "_type": "_doc",
                "_id": "22",
                "_score": 1.7963929,
                "_source": {
                    "productName": "Logitech PTZ Pro 2 Camera – USB HD 1080P Video Camera for Conference Rooms",
                    "description": "Ideal for conference rooms, training environments, large events and other Professional video applications\r\nDelivers Brilliantly sharp image resolution, outstanding color reproduction, and exceptional Optical accuracy\r\nEnhanced pan/tilt and zoom motor performance make moving from preset to preset smoother. The wide field of view make it Easy to see everyone clearly\r\nZoom wide or Zero in on close-ups to clearly view objects, whiteboard content, and other details\r\nAdvanced Camera technology frees up bandwidth by processing video within the PTZ Camera, resulting in a smoother video stream in applications like Microsoft Skype for business",
                    "categories": [
                        {
                            "name": "Data Networking"
                        }
                    ],
                    "id": 22,
                    "price": 78000.0,
                    "inStock": 12,
                    "available": true,
                    "created_at": "2020-06-11T01:10:21.585430+00:00",
                    "updated_at": "2020-06-11T01:10:21.585430+00:00"
                },
                "text": "Ideal for conference rooms, training environments, large events and other Professional video applications\r\nDelivers Brilliantly sharp image resolution, outstanding color reproduction, and exceptional Optical accuracy\r\nEnhanced pan/tilt and zoom motor performance make moving from preset to preset smoother. The wide field of view make it Easy to see everyone clearly\r\nZoom wide or Zero in on close-ups to clearly view objects, whiteboard content, and other details\r\nAdvanced Camera technology frees up bandwidth by processing video within the PTZ Camera, resulting in a smoother video stream in applications like Microsoft Skype for business"
            },
            {
                "_index": "products",
                "_type": "_doc",
                "_id": "36",
                "_score": 1.7699822,
                "_source": {
                    "productName": "Tenveo 3X Optical Zoom Professional Video Conference Camera, 1080P Full HD USB PTZ Webcam with Remote Control, for Live Streaming, Broadcasting, Business Group Meeting (TEVO-VX3-1080)",
                    "description": "【1080p HD Crystal Clear Video】:Professional enterprise-level 1080p full HD video conference camera with 3x optical zoom and 90°wide-angle for for small and simple group conferencing, making sure to display and transfer crystal clear video to everyone.\r\n【PTZ & Preset Operation】:IR remote control or meeting software can Easily control this PTZ camera. 3X optical manual/automatic zoom. Preset position Setting easily: supports 9 preset positions. Tips: press the button \"preset\" number (1-9) on the remote control to preset a position. Click 1-9 to call the reserved preset.\r\n【USB Plug and Play】:Power on, connect the camera to PC/laptop with USB cable, then open the meeting software, easy, fast and stable, you can start a video conference.\r\n【Perfect Compatibility with Meeting Software】: Tenveo conference camera works perfectly with Skype for Business, Zoom, Microsoft Lync, Google Meet, CISCO, WebEx and most of other video conference platforms. you can also use it for live streaming/ video studio with OBS, Xsplit, Streamlabs, Mixer, etc.\r\n【Packing List & Guarantee】:1*3x Zoom PTZ camera, 1*IR remote control (without battery), 1*DC-12V power adaptor, 1*USB2.0 cable (5m), 1*RS232C cable, 1*Ceiling mounting cap and screws, 1*manual, 1*mouse pad (gift). You enjoys 30-days return guarantee and 18-months warranty.",
                    "categories": [
                        {
                            "name": "Wireless Networking"
                        },
                        {
                            "name": "Data Networking"
                        }
                    ],
                    "id": 36,
                    "price": 25000.0,
                    "inStock": 10,
                    "available": true,
                    "created_at": "2020-06-16T04:42:29.512805+00:00",
                    "updated_at": "2020-06-16T04:42:29.512805+00:00"
                },
                "text": "【1080p HD Crystal Clear Video】:Professional enterprise-level 1080p full HD video conference camera with 3x optical zoom and 90°wide-angle for for small and simple group conferencing, making sure to display and transfer crystal clear video to everyone.\r\n【PTZ & Preset Operation】:IR remote control or meeting software can Easily control this PTZ camera. 3X optical manual/automatic zoom. Preset position Setting easily: supports 9 preset positions. Tips: press the button \"preset\" number (1-9) on the remote control to preset a position. Click 1-9 to call the reserved preset.\r\n【USB Plug and Play】:Power on, connect the camera to PC/laptop with USB cable, then open the meeting software, easy, fast and stable, you can start a video conference.\r\n【Perfect Compatibility with Meeting Software】: Tenveo conference camera works perfectly with Skype for Business, Zoom, Microsoft Lync, Google Meet, CISCO, WebEx and most of other video conference platforms. you can also use it for live streaming/ video studio with OBS, Xsplit, Streamlabs, Mixer, etc.\r\n【Packing List & Guarantee】:1*3x Zoom PTZ camera, 1*IR remote control (without battery), 1*DC-12V power adaptor, 1*USB2.0 cable (5m), 1*RS232C cable, 1*Ceiling mounting cap and screws, 1*manual, 1*mouse pad (gift). You enjoys 30-days return guarantee and 18-months warranty."
            },
            {
                "_index": "products",
                "_type": "_doc",
                "_id": "21",
                "_score": 1.762239,
                "_source": {
                    "productName": "Logitech MeetUp HD Video and Audio Conferencing System for Small Meeting Rooms",
                    "description": "Logitech's premier ConferenceCam specifically designed for business-grade video meetings in huddle rooms and small conference roomsSuper-Wide 120-degree field of view enables everyone in the room to be seen, even people sitting close to the camera or at the edges of the roomBuilt-In audio system features 3 microphones and a custom-tuned speaker specifically optimized for ultra-clear conversations in huddle roomsSupports the highest HD video quality for your network bandwidth and apps now and in the future with multiple video resolutions, including ultra 4k, 1080p and 720pDoubles as a speakerphone with an easy wireless connection to Bluetooth mobile devices. Microphone frequency response: 90hz - 16khz for full voice reproduction and high intelligibility without noise",
                    "categories": [
                        {
                            "name": "Desktops"
                        }
                    ],
                    "id": 21,
                    "price": 67000.0,
                    "inStock": 3,
                    "available": false,
                    "created_at": "2020-06-11T01:09:47.905014+00:00",
                    "updated_at": "2020-06-11T01:09:47.905014+00:00"
                },
                "text": "Logitech's premier ConferenceCam specifically designed for business-grade video meetings in huddle rooms and small conference roomsSuper-Wide 120-degree field of view enables everyone in the room to be seen, even people sitting close to the camera or at the edges of the roomBuilt-In audio system features 3 microphones and a custom-tuned speaker specifically optimized for ultra-clear conversations in huddle roomsSupports the highest HD video quality for your network bandwidth and apps now and in the future with multiple video resolutions, including ultra 4k, 1080p and 720pDoubles as a speakerphone with an easy wireless connection to Bluetooth mobile devices. Microphone frequency response: 90hz - 16khz for full voice reproduction and high intelligibility without noise"
            },
            {
                "_index": "products",
                "_type": "_doc",
                "_id": "43",
                "_score": 1.7493074,
                "_source": {
                    "productName": "Echo Dot (3rd gen) - Smart speaker with Alexa - Charcoal",
                    "description": "Meet Echo Dot - Our most popular smart speaker with a fabric design. It is our most compact smart speaker that fits perfectly into small spaces.\r\nImproved speaker quality - Better speaker quality than Echo Dot Gen 2 for richer and louder sound. Pair with a second Echo Dot for stereo sound.\r\nVoice control your music - Stream songs from Amazon Music, Apple Music, Sirius XM, Spotify, Deezer, and others. You can also listen to audiobooks from Audible.\r\nReady to help - Ask Alexa to play music, answer questions, read the news, check the weather, set alarms, control compatible smart home devices, and more.\r\nVoice control your smart home - Turn on lights, adjust thermostats, lock doors, and more with compatible connected devices.\r\nConnect with others - Call almost anyone hands-free. Instantly drop in on other rooms in your home or make an announcement to every room with a compatible Echo device.\r\nAlexa has skills - With many skills and counting, Alexa is always getting smarter and adding new skills like checking your flight, playing games, and more.\r\nDesigned to protect your privacy - Built with multiple layers of privacy protections and controls, including a microphone off button that electronically disconnects the microphones.",
                    "categories": [
                        {
                            "name": "Data Networking"
                        }
                    ],
                    "id": 43,
                    "price": 2999.0,
                    "inStock": 22,
                    "available": true,
                    "created_at": "2020-06-18T04:33:23.987867+00:00",
                    "updated_at": "2020-06-18T04:33:23.987867+00:00"
                },
                "text": "Meet Echo Dot - Our most popular smart speaker with a fabric design. It is our most compact smart speaker that fits perfectly into small spaces.\r\nImproved speaker quality - Better speaker quality than Echo Dot Gen 2 for richer and louder sound. Pair with a second Echo Dot for stereo sound.\r\nVoice control your music - Stream songs from Amazon Music, Apple Music, Sirius XM, Spotify, Deezer, and others. You can also listen to audiobooks from Audible.\r\nReady to help - Ask Alexa to play music, answer questions, read the news, check the weather, set alarms, control compatible smart home devices, and more.\r\nVoice control your smart home - Turn on lights, adjust thermostats, lock doors, and more with compatible connected devices.\r\nConnect with others - Call almost anyone hands-free. Instantly drop in on other rooms in your home or make an announcement to every room with a compatible Echo device.\r\nAlexa has skills - With many skills and counting, Alexa is always getting smarter and adding new skills like checking your flight, playing games, and more.\r\nDesigned to protect your privacy - Built with multiple layers of privacy protections and controls, including a microphone off button that electronically disconnects the microphones."
            },
            {
                "_index": "products",
                "_type": "_doc",
                "_id": "37",
                "_score": 1.736884,
                "_source": {
                    "productName": "Yealink SIP-T27G IP Phone Gigabit Ethernet PoE Support",
                    "description": "With faster response on the phones user interface and better device performance, the SIP-T27G IP phone, as the upgraded product of T27P, is Yealinks latest feature-rich tool unifying superior voice capabilities and increased function extension capability for business.\r\nYealink Optima HD technology and wideband codec of Opus deliver a superb sound quality and bring you a life-like voice communications.\r\nWith an all-new USB port, the SIP-T27G boasts unparalleled functionality and expansibility, which comes ready for future Bluetooth, Wi-Fi and USB recording features.\r\nSeamlessly migrated to GigE-based network infrastructure, SIP-T27G IP phone is also built with the Gigabit Ethernet facilitating very rapidly call handling.\r\nFor the perfect T27G phone it features Power Over Ethernet (PoE) and Headset (EHS) support",
                    "categories": [
                        {
                            "name": "Wireless Networking"
                        },
                        {
                            "name": "Data Networking"
                        }
                    ],
                    "id": 37,
                    "price": 6000.0,
                    "inStock": 2,
                    "available": true,
                    "created_at": "2020-06-17T05:13:57.754887+00:00",
                    "updated_at": "2020-06-17T05:13:57.754887+00:00"
                },
                "text": "With faster response on the phones user interface and better device performance, the SIP-T27G IP phone, as the upgraded product of T27P, is Yealinks latest feature-rich tool unifying superior voice capabilities and increased function extension capability for business.\r\nYealink Optima HD technology and wideband codec of Opus deliver a superb sound quality and bring you a life-like voice communications.\r\nWith an all-new USB port, the SIP-T27G boasts unparalleled functionality and expansibility, which comes ready for future Bluetooth, Wi-Fi and USB recording features.\r\nSeamlessly migrated to GigE-based network infrastructure, SIP-T27G IP phone is also built with the Gigabit Ethernet facilitating very rapidly call handling.\r\nFor the perfect T27G phone it features Power Over Ethernet (PoE) and Headset (EHS) support"
            },
            {
                "_index": "products",
                "_type": "_doc",
                "_id": "44",
                "_score": 1.6591692,
                "_source": {
                    "productName": "Cisco Systems Conference Camera - Color - 1920 x 1080 - HDMI",
                    "description": "New Open Box\r\nSame day shipping if purchased before 4PM CST, Monday through Friday\r\nRemaining manuf. warranty included. One year Core 4 Solutions warranty when no manuf. warr. remains\r\nPlease contact Core 4 Solutions for questions about expedited shipping, item condition, warranty etc.\r\nCore 4 Solutions has served over 18,900+ satisfied IT buyers.",
                    "categories": [
                        {
                            "name": "Wireless Networking"
                        }
                    ],
                    "id": 44,
                    "price": 5500.0,
                    "inStock": 3,
                    "available": true,
                    "created_at": "2020-06-19T05:24:26.485025+00:00",
                    "updated_at": "2020-06-19T05:24:26.485025+00:00"
                },
                "text": "New Open Box\r\nSame day shipping if purchased before 4PM CST, Monday through Friday\r\nRemaining manuf. warranty included. One year Core 4 Solutions warranty when no manuf. warr. remains\r\nPlease contact Core 4 Solutions for questions about expedited shipping, item condition, warranty etc.\r\nCore 4 Solutions has served over 18,900+ satisfied IT buyers."
            },
            {
                "_index": "products",
                "_type": "_doc",
                "_id": "39",
                "_score": 1.5538495,
                "_source": {
                    "productName": "Polycom Soundstation Ip5000 Sip Conference Phone",
                    "description": "Multilingual Menu/Echo Cancellation\r\nVoice Activity Detection/Comfort Noise Generation\r\nPacket Loss Concealment",
                    "categories": [
                        {
                            "name": "Data Networking"
                        }
                    ],
                    "id": 39,
                    "price": 18138.5,
                    "inStock": 2,
                    "available": true,
                    "created_at": "2020-06-17T06:14:37.866690+00:00",
                    "updated_at": "2020-06-17T06:14:37.866690+00:00"
                },
                "text": "Multilingual Menu/Echo Cancellation\r\nVoice Activity Detection/Comfort Noise Generation\r\nPacket Loss Concealment"
            },
            {
                "_index": "products",
                "_type": "_doc",
                "_id": "38",
                "_score": 1.4802003,
                "_source": {
                    "productName": "YEALINK VOIP ELEGANT IP PHONE SIP-T42S",
                    "description": "The SIP-T42S IP phone is a dynamic business communications tool for superior voice communications and extended functionality. It has a faster, more responsive interface than the T42G\r\n2.7-inch 192X64-pixel graphical LCD with backlight\r\nUSB2.0",
                    "categories": [
                        {
                            "name": "Mobile Phones"
                        }
                    ],
                    "id": 38,
                    "price": 9500.0,
                    "inStock": 3,
                    "available": true,
                    "created_at": "2020-06-17T05:22:52.502992+00:00",
                    "updated_at": "2020-06-17T05:22:52.502992+00:00"
                },
                "text": "The SIP-T42S IP phone is a dynamic business communications tool for superior voice communications and extended functionality. It has a faster, more responsive interface than the T42G\r\n2.7-inch 192X64-pixel graphical LCD with backlight\r\nUSB2.0"
            },
            {
                "_index": "products",
                "_type": "_doc",
                "_id": "45",
                "_score": 0.51149696,
                "_source": {
                    "productName": "another conference device",
                    "description": "another conference device",
                    "categories": [
                        {
                            "name": "Wireless Networking"
                        }
                    ],
                    "id": 45,
                    "price": 333.0,
                    "inStock": 3,
                    "available": true,
                    "created_at": "2020-06-19T05:24:59.056003+00:00",
                    "updated_at": "2020-06-19T05:24:59.056003+00:00"
                },
                "text": "another conference device"
            },
            {
                "_index": "products",
                "_type": "_doc",
                "_id": "47",
                "_score": 0.51149696,
                "_source": {
                    "productName": "One more mroe conference",
                    "description": "conference polycom  asd",
                    "categories": [
                        {
                            "name": "Wireless Networking"
                        }
                    ],
                    "id": 47,
                    "price": 333.0,
                    "inStock": 3,
                    "available": true,
                    "created_at": "2020-06-19T05:40:38.235484+00:00",
                    "updated_at": "2020-06-19T05:40:38.235484+00:00"
                },
                "text": "conference polycom  asd"
            }
        ],
        "length": 10,
        "offset": 0
    }
],
"_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
}

}

Ниже мое мнение (неполное)

enter code here



q = request.GET.get('q')
url1 = 'http://%s/search/products/functional_suggest/?product_match=%s' % (get_current_site(request), q)
response1 = requests.get(url1)
p1 = response1.json()
print(type(response1))
for s in p1['product_match'][0]['options']:
    products = s['_source']
    print(products)
context = {
    'products': products,
    'query': q,
}
return render(request, 'mainapp/search1.html', context)

Мой HTML шаблон использует a для l oop

{% для продукта в продуктах%}

            <div class="grid_1_of_4 images_1_of_4">

                 <a href="preview.html"><img src="{{newimage.image|thumbnail_url:'sm_display'}}" alt="" /></a>

                 <h2>{{product.productName|truncatechars:50}} </h2>
                <div class="price-details">
                   <div class="price-number">
                        <p><span class="rupees">&#8377;{{product.price}}</span></p>
                    </div>
                            <div class="add-cart">
                                <h4><a href="preview.html">Add to Cart</a></h4>
                             </div>
                         <div class="clear"></div>
                </div>
            </div>
          {% endfor %}

Я новичок в django и elasticsearch. Не уверен, что мне не хватает.

...