我有这个简单的功能来更新现有记录,方法是通过主键以某种形式获取该记录,然后在Django 2.0.1中的数据库中对其进行更新。我能够从数据库中很好地获取数据,但是当我按Submit to POST请求被发送到([17/Jun/2019 17:59:28] "POST /suppliers/lr/13/ HTTP/1.1" 200 25533
)时,但数据库中没有反映出任何变化。这是我的代码:
Views.py
@login_required
@supplier_required
def LrFormView (request, pk):
lr_object = get_object_or_404(LR, id=pk) # Or slug=slug
form = LrForm(data=request.POST or None, instance=lr_object)
if form.is_valid():
form.save() # redirect()
return render(request, 'classroom/suppliers/print_LR.html', {'form': form})
Forms.py
class LrForm(forms.ModelForm):
class Meta:
model = LR
fields = ('lr_declared', 'ewaybill_no', 'lr_quantity', 'lr_weight', 'lr_invoice_date', 'lr_item_name',
'lr_consignor_name', 'lr_consignor_address', 'lr_consignor_contact_name',
'lr_consignor_contact_phone', 'lr_consignee_name', 'lr_consignee_address',
'lr_consignee_contact_name', 'lr_consignee_contact_phone', 'vehicle_no', 'lr_vehicle_type',
'driver_name', 'driver_no', 'lr_no', 'lr_date', 'lr_billingparty', 'invoice_no')
models.py:
class LR(models.Model):
lr_quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE, related_name='quiz_lr')
lr_no = models.IntegerField(default=0)
lr_date = models.DateTimeField(max_length=255, default=now)
lr_billingparty = models.CharField(max_length=255, default=0) # supplier company name foreign key
invoice_no = models.IntegerField(default=0)
lr_declared = models.IntegerField(default=0)
ewaybill_no = models.IntegerField(default=0)
lr_quantity = models.IntegerField(default=0) # quantity
lr_weight = models.IntegerField(default=0) # weight
lr_invoice_date = models.DateTimeField(max_length=255, default=now)
lr_item_name = models.CharField(max_length=255, default=0) # said to contain
lr_consignor_name = models.CharField(max_length=255, default=0) # shipper company name
lr_consignor_address = models.CharField(max_length=255, default=0) # shipper company name
lr_consignor_contact_name = models.CharField(max_length=255, default=0) # shipper company name
lr_consignor_contact_phone = models.CharField(max_length=255, default=0) # shipper company name
lr_consignee_name = models.CharField(max_length=255, default=0) # shipper company name
lr_consignee_address = models.CharField(max_length=255, default=0) # shipper company name
lr_consignee_contact_name = models.CharField(max_length=255, default=0) # shipper company name
lr_consignee_contact_phone = models.CharField(max_length=255, default=0) # shipper company name
vehicle_no = models.CharField(max_length=255, default=0)
lr_vehicle_type = models.IntegerField(default=0)
driver_name = models.CharField(max_length=255, default=0)
driver_no = models.IntegerField(default=0)
urls.py
path('lr/<int:pk>/', suppliers.LrFormView, name='lr_form'),
我将通过以下按钮转到此页面:
<a href="{% url 'suppliers:lr_form' taken_quiz.quiz_id %}" class="btn btn-primary">Generate LR</a>
我尝试在Internet上查找,并发现了无数使用此方法更新数据库中记录的示例。我无法理解这里出了什么问题。我应该如何调试呢?