Home > database >  Django Template extract list
Django Template extract list

Time:04-13

I have a list:

  • productlinks = [google.com, tes.com, lol.com]
  • prices = [$134, $123,$123]

I wanna extract those list to my django template inside the bootsrap. Here is my django template code:

<section >
        <div >
            <div >
                {% for link in productlinks %}
                <div >
                    <div >
                        <!-- Product image-->
                        <img  src="https://dummyimage.com/450x300/dee2e6/6c757d.jpg" alt="..." />
                        <!-- Product details-->
                        <div >
                            <div >
                                <!-- Product name-->
                                <h5 >Fancy Product</h5>
                                <!-- Product price--> x  
                                $40.00 - $80.00 -> price must be here using loop to extract the list
                            </div>
                        </div>
                        <!-- Product actions-->
                        <div >
                            
                            <div ><a  href="{{ link }}" target="_blank">View options</a></div>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>
        </div>
    </section>

So basically, I wanna also extract the prices list but i dont know how to place the loop after the my productlinks loop. Because it will ruins the result.

and here is my views code:

return render(request, '/home.html', {
            'productlinks': productlinks,
            'prices': productprices
            }
        )

CodePudding user response:

views

return render(request, '/home.html', {
            'data': zip(productlinks,productprices)
            }
        )

template

<section >
        <div >
            <div >
                {% for link,price in data %}
                <div >
                    <div >
                        <!-- Product image-->
                        <img  src="https://dummyimage.com/450x300/dee2e6/6c757d.jpg" alt="..." />
                        <!-- Product details-->
                        <div >
                            <div >
                                <!-- Product name-->
                                <h5 >Fancy Product</h5>
                                <!-- Product price--> x  
                                {{ price }}
                            </div>
                        </div>
                        <!-- Product actions-->
                        <div >
                            
                            <div ><a  href="{{ link }}" target="_blank">View options</a></div>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>
        </div>
    </section>
  • Related