当我尝试将数据编码为json时发生错误。错误是由于分页造成的。我想要json编码数据。
这是我用来从db获取结果然后将其编码为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)
任何帮助都会非常感激。
由于
答案 0 :(得分:1)
我已经解决了这个问题。我刚刚在json中编码数据时添加了“object_list”,如下所示:
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)