Hopefully, the Django paging component needs to receive an iterable object, so the incoming data must be an iterable object, such as a list, dictionary, etc. If you pass in a two-dimensional array, you may cause the paging component to fail.
This is the code for the paging component, also learned on the Internet
from django.utils.safestring import mark_safe
class Pagination(object):
def __init__(self, request, queryset, page_size=15, page_param="page", plus=5):
from django.http.request import QueryDict
import copy
query_dict = copy.deepcopy(request.GET)
query_dict._mutable = True
self.query_dict = query_dict
self.page_param = page_param
page = request.GET.get(page_param, "1")
if page.isdecimal():
page = int(page)
else:
page = 1
self.page = page
self.page_size = page_size
self.start = (page - 1) * page_size
self.end = page * page_size
self.page_queryset = queryset[self.start:self.end]
total_count = len(queryset)
total_page_count, div = divmod(total_count, page_size)
if div:
total_page_count += 1
self.total_page_count = total_page_count
self.plus = plus
def html(self):
if self.total_page_count <= 2 * self.plus + 1:
start_page = 1
end_page = self.total_page_count
else:
if self.page <= self.plus:
start_page = 1
end_page = 2 * self.plus + 1
else:
if (self.page + self.plus) > self.total_page_count:
start_page = self.total_page_count - 2 * self.plus
end_page = self.total_page_count
else:
start_page = self.page - self.plus
end_page = self.page + self.plus
page_str_list = []
self.query_dict.setlist(self.page_param, [1])
page_str_list.append('首页 '.format(self.query_dict.urlencode()))
if self.page > 1:
self.query_dict.setlist(self.page_param, [self.page - 1])
prev = '上一页 '.format(self.query_dict.urlencode())
else:
self.query_dict.setlist(self.page_param, [1])
prev = '上一页 '.format(self.query_dict.urlencode())
page_str_list.append(prev)
for i in range(start_page, end_page + 1):
self.query_dict.setlist(self.page_param, [i])
if i == self.page:
ele = '{} '.format(self.query_dict.urlencode(), i)
else:
ele = '{} '.format(self.query_dict.urlencode(), i)
page_str_list.append(ele)
if self.page < self.total_page_count:
self.query_dict.setlist(self.page_param, [self.page + 1])
prev = '下一页 '.format(self.query_dict.urlencode())
else:
self.query_dict.setlist(self.page_param, [self.total_page_count])
prev = '下一页 '.format(self.query_dict.urlencode())
page_str_list.append(prev)
self.query_dict.setlist(self.page_param, [self.total_page_count])
page_str_list.append('尾页 '.format(self.query_dict.urlencode()))
page_string = mark_safe("".join(page_str_list))
return page_string
Here is my view function for passing a two-dimensional array
#小波变换
def www(request):
file_object = request.FILES.get("file")
if file_object:
data = xlrd.open_workbook(filename=None, file_contents=file_object.read())
sheet = data.sheet_by_index(0)
nrows = sheet.nrows
list = []
for i in range(1, nrows):
list.append(np.array(sheet.row_values(i)))
arr = np.array(list)
p = pre()
queryset = p.wave(arr)
queryset = np.round(queryset,6)
page_object = Pagination(request, queryset) ########加这一句分页
context = {"queryset": page_object.page_queryset, # 分完页的数据
"page_string": page_object.html() # 页码
}
return render(request, 'wave2.html', context)
return render(request,'wave.html')
>
0 Answer
Hopefully, the Django paging component needs to receive an iterable object, so the incoming data must be an iterable object, such as a list, dictionary, etc. If you pass in a two-dimensional array, you may cause the paging component to fail.
This answer quotes chatgpt, see if it works
Themay be in the init function of the paging component, because in this function, it uses the queryset[self.start:self.end] to get the paging data, where the queryset should be a list, When you pass in a two-dimensional array, it may not be properly converted to a list dictionary, causing the paging component to process the data incorrectly.
To solve this problem, you can convert a two-dimensional array into a list dictionary before passing in the data. For example, you could do this in a view function:
def www(request):
file_object = request.FILES.get("file")
if file_object:
data = xlrd.open_workbook(filename=None, file_contents=file_object.read())
sheet = data.sheet_by_index(0)
nrows = sheet.nrows
list = []
for i in range(1, nrows):
list.append(dict(zip(range(sheet.ncols), sheet.row_values(i))))
queryset = np.array(list)
p = pre()
queryset = p.wave(queryset)
queryset = np.round(queryset,6)
page_object = Pagination(request, queryset)
context = {"queryset": page_object.page_queryset,
"page_string": page_object.html()
}
return render(request, 'wave2.html', context)
return render(request,'wave.html')
Here we use dict(zip(range(sheet.ncols), sheet.row_values(i))) to convert a row of data to a dictionary and add all the dictionaries to the list, thus converting a two-dimensional array to the form of a list-set dictionary
thanks for brother answer! But now the problem is that p=pre() is an object I instantiated, and this module requires that the data passed in must be an array. The function of this page is to display the preprocessed data of this module to the web page, so you see if there is a way to display the preprocessed data of this module after queryset = np.round(queryset,6), Convert a two-dimensional array into a list dictionary.
这家伙很懒,什么都没留下...