[Answer]-Django: template forloop multiple dictionaries

1👍

How about something like this:

View:

def store(request, store_id=1):
    store = Store.objects.get(StoreID=store_id) 

    args = {}
    args['store'] = store

    return render(request, 'store.html', args)

Template:

<pre>
    <code>
        {% if store.storeliquor_set.count %}
        <table class="sortable">
            <thead>
                <tr>
                    <th scope="col">Liquor Code</th>
                    <th scope="col">Brand Name</th>
                    <th scope="col">Vendor Name</th>
                </tr>
            </thead>
            <tbody>
                {% for storeliquor in store.storeliquor_set.all %}
                <tr>
                    <td>{{ storeliquor.liquorID.LiquorCode }}</td>
                    <td><a href="/stores/storeliquors/{{ a.StoreID_id }}/{{ liquor.liquorID_id }}/">{{ liquor.liquorID.BrandName }}</a></td>
                    <td>{{ storeliquor.liquorID.VendorName }}</td>
                    <td>{{ storeliquor.StorePrice }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>

        {% else %}

        <p>None to show!</p>

        {% endif %}
    </code>
</pre>

Although I would recommend you to change your model to something like this (according to the python style guide):

class StoreLiquor(models.Model):
    liquor = models.ForeignKey(Liquor)
    store = models.ForeignKey(Store)
    price = models.DecimalField('Store Price', max_digits=5, decimal_places=2)

class Liquor(models.Model):
    liquor_code = models._positiveSmallIntegerField('Liquor Code', max_length =5)
    brand_name = models.CharField('Brand Name', max_length =32)
    ada_number = models._positiveSmallIntegerField('ADA Number', max_length =3)
    ada_name = models.CharField('ADA Name', max_length =25)
    vendor_name = models.CharField('Vendor Name', max_length =25)
    liquor_type = models.CharField('ADA Name', max_length =20)
    proof = models.DecimalField(max_digits =3, decimal_places =1)
    bottle_size = models.CharField('Bottle Size', max_length =7)
    pack_size = models._positiveSmallIntegerField('PackSize', max_length =3)
    on_premise_price = models.DecimalField('On Premise Price', max_digits =5, decimal_places =2)
    off_premise_price = models.DecimalField('Off Premise Price', max_digits =5, decimal_places =2)
    shelf_price = models.DecimalField('Shelf Price', max_digits =5, decimal_places =2)
    global_trade_item_number_1 = models.BigIntegerField('Global Trade Item Number 1', max_length =14)
    global_trade_item_number_2 = models.BigIntegerField('Global Trade Item Number 2', max_length =14)

class Store(models.Model):
    pass
👤Wolph

Leave a comment