Django:模板forloop多个词典

时间:2013-08-29 23:45:21

标签: django dictionary django-models django-templates django-views

我需要加载与商店相关的酒的价格,以及普通酒类数据库中的其他数据。我知道如何加载数据,以及如何在两个不同的表中显示信息,但我需要将商店价格简单地作为现有表中的另一行,我不知道如何做到这一点。以下是观点:

def store(request, store_id=1):

    a = Store.objects.get(StoreID=store_id) 
    b = StoreLiquor.objects.filter(storeID__exact=a).values_list('liquorID', flat=True)
    x = StoreLiquor.objects.filter(storeID_exact=a)

    args = {}

    args['liquors'] = Liquor.objects.filter(id__in=b)
    args['prices'] = x
    args['a'] = a


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

这是html:

<pre>
    <code>
        {% if liquors.count > 0 %}
            <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 liquor in liquors %}
                        <tr>
                            <td>{{ liquor.LiquorCode }}</td>
                            <td><a href="/stores/storeliquors/{{ a.StoreID }}/{{ liquor.id }}/">{{ liquor.BrandName }}</a></td>
                            <td>{{ liquor.VendorName }}</td>
                            <td>{{ liquor.StorePrice }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        {% else %}
            <p>None to show!</p>
        {% endif %}
    </code>
</pre>

型号:

    class StoreLiquor(models.Model):
        StoreLiquorID = models.AutoField(primary_key=True)
        liquorID = models.ForeignKey(Liquor)
        storeID = models.ForeignKey(Store)
        StorePrice = models.DecimalField('Store Price', max_digits=5, decimal_places=2)

    class Liquor(models.Model):
        LiquorCode = models.PositiveSmallIntegerField('Liquor Code', max_length=5)
        BrandName = models.CharField('Brand Name', max_length=32)
        ADANumber = models.PositiveSmallIntegerField('ADA Number', max_length=3)
        ADAName = models.CharField('ADA Name', max_length=25)
        VendorName = models.CharField('Vendor Name', max_length=25)
        LiquorType = models.CharField('ADA Name', max_length=20)
        Proof = models.DecimalField(max_digits=3, decimal_places=1)
        BottleSize = models.CharField('Bottle Size', max_length=7)
        PackSize = models.PositiveSmallIntegerField('PackSize', max_length=3)
        OnPremisePrice = models.DecimalField('On Premise Price', max_digits=5, decimal_places=2)
        OffPremisePrice = models.DecimalField('Off Premise Price', max_digits=5, decimal_places=2)
        ShelfPrice = models.DecimalField('Shelf Price', max_digits=5, decimal_places=2)
        GlobalTradeItemNumber1 = models.BigIntegerField('Global Trade Item Number 1', max_length=14)
        GlobalTradeItemNumber2 = models.BigIntegerField('Global Trade Item Number 2', max_length=14)

    class Store(models.Model):
        StoreID = models.AutoField(primary_key=True)

1 个答案:

答案 0 :(得分:1)

这样的事情怎么样:

查看:

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

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

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

模板:

<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>

虽然我建议你把模型改成这样的东西(根据python样式指南):

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