Django:用于循环多个字典的模板
我需要加载与商店关联的酒的价格,以及通用酒数据库中的其他数据。我知道如何在两个不同的表中加载数据,以及如何在信息中显示,但我只需要将存储价格设置为现有表中的另一行,我不确定如何做到这一点。以下是视图:Django:用于循环多个字典的模板,django,dictionary,django-models,django-templates,django-views,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('liquo
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)
<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):
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
以下是html:
像这样的怎么样: 视图: 模板:
你能添加模型定义吗?@Wolph,我添加了它们。缺少商店模型@dan klasson,我添加了它,但我认为,因为这里只使用了ID字段,所以我怀疑你是否需要它。商店模型中还定义了其他东西,比如地址和用户的外键。但无论如何,我在这行的模板上得到了一个错误{%for store.storeliquis\u set%}“我收到一个类型错误,'RelatedManager'对象不可编辑”“我的坏…”。。。最后应该有一个
.all
。实际上我已经修好了,你必须添加一个.all{store.storeliquis_set.all%}
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>
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