我在分页期间遇到了同样的问题。对于分页,您不需要在模态中创建函数。
你可以使用 LIMT,同样的想法可以通过简单的我的泛化函数来完成
def testPagination(请求):
dataObj = paginationData('Item',page_no,10)
data["dataIndex"] = dataObj["dataIndex"]
data["page_no"] = dataObj["page_no"] #given page number will helps to active link at template
data["data"] = dataObj["data"] #actual data will helps in template
data['pagination'] = dataObj["pagination"] #Will helps to make dynamic link for pagination
泛化分页功能
def paginationData(modalName,pageNo,perPage):
data = {} #initialize variables
Modal = getModel(modalName) # Function to get modal reference
count = Modal.objects.count()#Calculate no of records
no_of_record_per_page = ceil(float(count) / perPage)
print ",Records counts : " + str(no_of_record_per_page)
pages = [] #No of pages
pages.append(1)
a = 1
while a < (no_of_record_per_page):
a += 1 # Same as a = a + 1
pages.append(a)
data["pagination"] = pages # pagenation for display link of page
data["page_no"] = pageNo
data['data'] = Modal.objects.all()[((pageNo-1)*perPage):(pageNo*perPage)]
data['dataIndex'] = str((pageNo -1)*perPage)
return data