"LoadMore" button on "Category product list" does not sort products by category

I did "Load More" button for "Product list" with tutorial and trying do like this on "Category product list", but it doest work normal.

My files:

Views.py

def product_list(request):
    total_data = Product.objects.count()
    data = Product.objects.all().order_by('-id')[:1]
    cats = Product.objects.distinct().values('category__title', 'category__id')
    return render(request, 'product_list.html',
                  {
                      'data': data,
                      'cats': cats,
                      'total_data': total_data,
                  })

def category_product_list(request, cat_id):
    category = Category.objects.get(id=cat_id)
    total_data = Product.objects.filter(category=category).count()
    data = Product.objects.filter(category=category).order_by('-id')[:1]
    return render(request, 'category_product_list.html', {'data': data, 'total_data': total_data})

def load_more_data(request):
    offset = int(request.GET['offset'])
    limit = int(request.GET['limit'])
    data = Product.objects.all().order_by('-id')[offset:offset+limit]
    t = render_to_string('ajax/product-list.html', {'data': data})
    return JsonResponse({'data': t})

def load_more_dataCat(request):
    offset = int(request.GET['offset'])
    limit = int(request.GET['limit'])
    data = Product.objects.filter(category=True).order_by('-id')[offset:offset+limit]
    t = render_to_string('ajax/category_product_list.html', {'data': data})
    return JsonResponse({'data': t})

custom.js

$(document).ready(function(){
    $("#loadMore").on('click', function(){
        var _currentProducts = $(".product-box").length;
        var _limit = $(this).attr('data-limit');
        var _total = $(this).attr('data-total');
        console.log(_total)

        // Start Ajax
        $.ajax({
            url:'/load-more-data',
            data:{
                limit: _limit,
                offset: _currentProducts
            },
            dataType:'json',
            beforeSend:function(){
                $("#loadMore").attr('disabled',true);
                $(".load-more-icon").addClass('fa-spin');
            },
            success:function(res){
                $("#filteredProducts").append(res.data);
                $("#loadMore").attr('disabled',false);
                $(".load-more-icon").removeClass('fa-spin');

                var _totalShowing = $(".product-box").length;
                if(_totalShowing==_total){
                    $("#loadMore").remove();
                }
            }
        });
        // End
    });
});

$(document).ready(function() {
    $("#loadMoreCat").on('click', function () {
        var _currentProducts = $(".product-box").length;
        var _limit = $(this).attr('data-limit');
        var _total = $(this).attr('data-total');
        console.log(_total)

        // Start Ajax
        $.ajax({
            url:'/load-more-dataCat',
            data:{
                limit: _limit,
                offset: _currentProducts
            },
            dataType:'json',
            beforeSend:function(){
                $("#loadMoreCat").attr('disabled',true);
                $(".load-more-icon").addClass('fa-spin');
            },
            success:function(res){
                $("#categoryProducts").append(res.data);
                $("#loadMoreCat").attr('disabled',false);
                $(".load-more-icon").removeClass('fa-spin');

                var _totalShowing = $(".product-box").length;
                if(_totalShowing==_total){
                    $("#loadMoreCat").remove();
                }
            }
        });
        // End
    });
});

When I using button on "Product list" page it showing more products from all products, but on "Category product list" it loading product from anoyher category, and same product in any category list.

I tried change views.py. I tried :

data = Product.objects.filter(category=True).order_by('-id')[offset:offset+limit] data = Product.objects.filter(category=category).order_by('-id')[offset:offset+limit]

and different combinations, but it's just changing problem. But, maybe, problem in another thing.

Back to Top