How to make a pagination with django and ajax/jquery

In this article, we'll code a simple Django pagination by using ajax/jquery.


class Articles(models.Model):
    title = models.CharField(max_length=300)
    def __str__(self):
        return self.title

from django.shortcuts import render
from django.http import JsonResponse
from django.core.paginator import Paginator
from .models import Articles

def pagination_pro(request):
    my_model = Articles.objects.all()
    #number of items on each page
    number_of_item = 10
    paginatorr = Paginator(my_model, number_of_item)
    #query_set for first page
    first_page =
    #range of page ex range(1, 3)
    page_range = paginatorr.page_range

    context = {
    if request.method == 'POST':
        page_n = request.POST.get('page_n', None) #getting page number
        results = list('id', 'title'))
        return JsonResponse({"results":results})

    return render(request, 'index.html',context)

from django.urls import path
from core.views import *

urlpatterns = [
    path('pagination/', pagination_pro, name='pagination_p'),


        <title>How to make a pagination with django and jquery</title>

<!-- queryset for first page -->
<div id="posts">
    {% for i in first_page %}
    {% endfor %}

<!-- loop pagination -->
{% for i in page_range %}
<a style="margin-left: 5px; font-size: 20px;" href="{{i}}">{{i}}</a>
{% endfor %}

    var page_n = $(this).attr('href');
    // ajax
                type: "POST",
                url: "{% url 'pagination_p' %}", // name of url
                data : {    
                page_n : page_n, //page_number
                csrfmiddlewaretoken: '{{ csrf_token }}',
            success: function (resp) {
               $.each(resp.results, function(i, val) {
                 //apending posts
                $('#posts').append('<h2>' + val.title + '</h2>')
            error: function () {}
        }); // 



How to make a pagination with django and jquery

English today is not an art to be mastered it's just a tool to use to get a result