Django分页数据不可序列化

时间:2016-04-21 05:45:50

标签: ajax django pagination

我正在尝试使用ajax post请求实现无限的分页。

我的ajax查询:

var product_id = {{product_id|safe}};
var category_id = {{category_id|safe}};
var subcategory_id = {{subcategory_id|safe}};
var ajax_type = {{ajax_type|safe}};
var url_name = {{url_name|safe}};
var page_no = {{page_no|safe}};
$.ajax({
    url : url_name,
    type : "POST",
    async : false,
    data : {action:ajax_type,
            product_id:product_id,
            category_id:category_id,
            subcategory_id:subcategory_id,
            page_no:page_no},

   success : function(response) {
       var new_products_list = response.products;
       page_no = response.page;
       .....

我的观点:

category_id = request.POST.get('category_id')
subcategory_id = request.POST.get('subcategory_id')
page = int(request.POST.get('page_no'))
product_data = Products().fetchProducts(category_id, subcategory_id)
paginator = Paginator(product_data, 12)
products = paginator.page(page)
print products
page = page + 1
return HttpResponse(json.dumps({'products':products,'page':page}), content_type='application/json')

我要做的是传递一些变量,包括url到html视图。当html加载时,ajax调用被发送到django视图以获取一些产品数据。我可以在django视图中访问从ajax传递的所有变量。

现在,当我对数据进行分页并使用print products进行打印时,它会打印<Page 1 of 1>,当我通过HttpResponse将响应传递回ajax时,会出现错误<Page 1 of 1> is not JSON serializable。我做错了什么?如何将分页数据作为响应传递回ajax函数?

还有什么可能是使用ajax分页数据的写方式?

0 个答案:

没有答案