我有两个django模型Calculations
和{
观点如下:
def archive(request):
calculations = Calculations.objects.filter(user=request.user)
calculation_data = []
for calculation in calculations:
customer_fullname, car_chassis, car_registration = ('', '', '')
if calculation.customer_id is not None:
customer = Customer.objects.get(id=calculation.customer_id)
customer_fullname = '{} {}'.format(customer.firstname, customer.lastname)
car_chassis = customer.chassis
car_registration = customer.registration
calculation_data.append({
'calculation': calculation,
'price': price_incl_vat(calculation.purchase_price),
'customer_fullname': customer_fullname,
'car_chassis': car_chassis,
'car_registration': car_registration,
})
context = {'calculation_data': calculation_data}
return render(request, 'master/archive.html', context)
并且在archive.html
I循环通过calculation_data
如下:
一切都很好。
现在我想实现分页。
根据文件,可以这样做:
calculations = Calculations.objects.all()
paginator = Paginator(calculations, 25)
但是如何实现对我的calculation_data
的分页呢?因为我在模板中循环的所有数据都被存储了。
有没有更好的方法来编写查询来完成分页?
提前谢谢。
根据docs,您可以将一个列表传递给Paginator,以便对计算数据执行相同的操作:
相关问题 更多 >
编程相关推荐