我已经编写了以下代码。我遇到的问题是数据库没有更新,也没有创建新记录。我假设我检索 POST 的方式不正确,但这就是为什么我要问我从哪里开始调试?我已经为数量、动作、构建和测试创建了模板变量。例如,当我尝试在 html {{ test }} 中调用它们时,即使是硬编码的也没有任何显示。我使用了萤火虫,表格确实发布了应该发布的值。该表单由两个下拉菜单组成,一个用于操作,一个用于构建。一个数字输入数量、一个文本框和一个提交按钮。如果有人可以给我一些建议,将不胜感激。我真的不明白为什么硬编码的没有出现如果您需要更多信息,请告诉我。
def update(request,Type_slug, slug, id):
error = False
Slug = slug
ID = id
Type = Type_slug
test = 'test'
quantity = request.POST['Qty']
action = request.POST['Action']
building = request.POST['Building']
comments = request.POST['Comments']
if Type == 'Chemicals':
item = Chemicals.objects.filter(S_field=Slug, id= ID)
New_Record = ChemicalRecord(Name=item.Name,Barcode=item.Barcode,Cost=item.Cost,Action=action,Building=building)
if building == 'Marcus':
building_two = 'Pettit'
elif building =='Pettit':
building_two ='Marcus'
Record_one = ChemicalRecord.objects.filter(Barcode=New_Record.Barcode).filter(Building=New_Record.Building)
if Record_one:
Record_one = ChemicalRecord.objects.filter(Barcode=New_Record.Barcode).filter(Building=New_Record.Building).latest('id')
New_Record.On_hand = Record_one.On_hand
else:
New_Record.On_hand = 0
if action == 'Receiving':
New_Record.On_hand = New_Record.On_hand+quantity
elif action == 'Removing':
New_Record.On_hand = New_Record.On_hand-quantity
Record_two = ChemicalRecord.objects.filter(Barcode=New_Record.Barcode).filter(Building=building_two)
if Record_two:
Record_two = ChemicalRecord.objects.filter(Barcode=New_Record.Barcode).filter(Building=building_two).latest('id')
Record_two_qty = Record_two.On_hand
else:
Record_two_qty = 0
New_qty = New_Record.On_hand+Record_two_qty
Chemicals.objects.filter(Barcode=obj.Barcode).update(On_hand=New_qty)
New_Record.save()