The error is occurred when I am trying to encode data into json. The error is due to pagination. I want json encoded data.
Here's the code that I am using for fetching the results from db and then encoding it into json
views.py
def pagination(obj, request):
max = request.GET.get('max') if 'max' in request.GET else 1
paginator = Paginator(obj, max) # Show 25 contacts per page
page = request.GET.get('page')
try:
return paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
return paginator.page(1)
except EmptyPage:
return ""
def folder_list(request):
folders = UserFolder.objects.filter(creator=request.user).order_by('-modified_date')
folders_dict = []
for folder in folders:
d = {}
for k, v in folder.__dict__.items():
d[k] = str(v)
d["creator__first_name"] = folder.creator.first_name
folders_dict.append(d)
folders = Common.pagination(folders_dict, request)
folders = json.dumps(folders)
return HttpResponse(folders)
Any help would be really appreciated.
Thanks
I have sorted out the issue. I have just added "object_list" while encoding the data in json like this :
def folder_list(request):
folders = UserFolder.objects.filter(creator=request.user).order_by('-modified_date')
folders_dict = []
for folder in folders:
d = {}
for k, v in folder.__dict__.items():
d[k] = str(v)
d["creator__first_name"] = folder.creator.first_name
folders_dict.append(d)
folders = Common.pagination(folders_dict, request)
folders = json.dumps(folders.object_list) #added .object_list
return HttpResponse(folders)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.