在我的Django视图中,我正在构建一个我想在模板中阅读的列表
我的列表名称是term_list
,我在里面附加for循环
def lease_payment_details(request, pk):
lease_id = pk
if lease_id:
try:
lease = get_object_or_404(Lease, pk=lease_id)
try:
activeterm = LeaseTerm.objects.get(lease=lease)
start = activeterm.start_period.order_value
end = activeterm.end_period.order_value
period_list = Period.objects.filter(order_value__lte = start, order_value__gte = end)
total = 1000
for obj in period_list:
obj.discount = 0
obj.amount = activeterm.amount
obj.topay = total - obj.amount - obj.discount
term_list.append(obj)
payment = activeterm.leasepayment_set.all().order_by('payment_date')
except LeaseTerm.DoesNotExist:
activeterm = None
payment = None
except ValueError:
raise Http404get_object_or_404(Lease, pk=lease_id)
else:
lease = "None"
activeterm = "None"
payment = "None"
term_list = "None"
data = {
'lease': lease, 'payment': payment, 'activeterm':activeterm,'term_list':term_list,
}
return render(request, 'lease/lease_payment_details.html', {'data': data})
但我收到此错误
分配前引用的局部变量'term_list'
我做错了什么?
答案 0 :(得分:0)
在执行term_list.append(obj)
之前,您必须将term_list声明为包含term_list = list()
或term_list = []
的列表。