正如您在下面的 html 中看到的,我的代码从数据中生成了一个表格,每行都有一个表格。当您提交表单时,它会根据它所在的行发布数据。这很好用。但是,我需要字段中的初始数据对于每一行都不同。它必须是基于行关联的数据的计算值。是否有一些 django 模板代码可以用来设置它?或者有没有办法可以从视图中传递该值?
{% for liquor in filter %}
<tr>
<td>{{ liquor.LiquorCode }}</td>
<td><a href="/liquors/get/{{ a.StoreID }}/{{ liquor.id }}/{{ SPI }}/">{{ liquor.BrandName }}</a></td>
<td>{{ liquor.VendorName }}</td>
<td>{{ liquor.LiquorType }}</td>
<td>{{ liquor.Proof }}</td>
<td>{{ liquor.BottleSize }}</td>
<td>{{ liquor.PackSize }}</td>
<td>{{ liquor.OffPremisePrice }}</td>
<td>{{ liquor.ShelfPrice }}</td>
<td>{{ liquor.GlobalTradeItemNumber1 }}</td>
<td><form action="/stores/quickadd/{{ a.StoreID }}/{{ liquor.id }}/{{ SPI }}/" method="post">{% csrf_token %}
{{form.as_p}}
<input type="submit" name="submit" value="Add to Store"></td>
</tr>
{% endfor %}
这是我的看法:
def product_list(request, store_id):
store = Store.objects.get(StoreID=store_id)
f = ProductFilter(request.GET, queryset=Liquor.objects.all())
LiqSPI = StoreLiquor.objects.filter(storeID=store_id).count()
AddLiqForm = AddLiquorForm()
args = {}
args['filter'] = f
args['a'] = store
args['SPI'] = LiqSPI + 1
args['form'] = AddLiqForm
return render(request,'UPC_filter.html', args)
def quickadd(request, store_id, liquor_id, SPI):
storeID = Store.objects.get(StoreID=store_id)
liquorID = Liquor.objects.get(id=liquor_id)
if request.method == "POST":
AddLiqForm = AddLiquorForm(request.POST)
if AddLiqForm.is_valid():
StoreLiqID = AddLiqForm.save(commit=False)
StoreLiqID.storeID = storeID
StoreLiqID.liquorID = liquorID
StoreLiqID.StorePrice = request.POST.get('StorePrice', '')
StoreLiqID.SPI = SPI
StoreLiqID.save()
return HttpResponseRedirect('/stores/UPC_Scan/%s' % store_id)