我正在尝试接收收据,该收据从Django数据库中获取数据并将其呈现在模板上。数据已存储,并且可以在终端中显示数据已正确存储在该模型中。我已经做过几次了,但是我无法弄清楚为什么模板中没有渲染数据。
这是我的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)
def str (self):
return self.name
这是我的views.py:
def PrintLR (request, pk):
LR = get_object_or_404(LR, lr_quiz_id=pk)
context_object_name = 'LR'
template_name = 'classroom/suppliers/showLR.html'
return render(request, 'classroom/suppliers/showLR.html', {LR:'LR'})
这是我的HTML模板:
<tr>
<th>CNOTE NO</th>
<th>BOOKING DATE</th>
<th>BILLING PARTY</th>
</tr>
<tr>
<td>{{ LR.lr_consignor_contact_phone }}</td>
<td>{{ LR.lr_declared }}</td>
<td>{{ LR.lr_consignee_name }}</td>
</tr>
在模板中看不到任何数据,而是字段为空。
答案 0 :(得分:0)
更改PrintLR
方法
def PrintLR (request, pk):
qwe = get_object_or_404(LR, lr_quiz_id=pk)
template_name = 'classroom/suppliers/showLR.html'
return render(request, 'classroom/suppliers/showLR.html', {qwe:'qwe'})
首先,确保它返回一个对象。
qwe = get_object_or_404(LR, lr_quiz_id=pk)
然后进入模板
<td>{{ qwe.lr_consignor_contact_phone }}</td>
<td>{{ qwe.lr_declared }}</td>
<td>{{ qwe.lr_consignee_name }}</td>
答案 1 :(得分:0)
此:
SVG
是错误的-您将模型实例用作键,并将文字字符串return render(request, 'classroom/suppliers/showLR.html', {LR:'LR'})
用作值,因此模板上下文中没有任何名为“ LR”的东西。你想要的是
'LR'
请注意,这次我们使用字符串作为键,使用实例作为值。
请注意,您不应为实例命名与类相同的名称-充其量是令人困惑的,并且会导致意外行为,因为它在当前作用域的其余部分隐藏了该类。哦,是的:return render(request, 'classroom/suppliers/showLR.html', {'LR':LR})
部分完全没有用。