Пагинация на страницах Категории не работает

Я сделал приложение для блога, в котором я хочу добавить пагинацию как на главной странице, так и на странице категории. но после долгих усилий я не понял, как использовать ее на странице категории. Я использую 2 параметра в представлении категории и не знаю, как это исправить. Всякий раз, когда я нажимаю на категорию, появляется сообщение "That page number is not an integer".

views.py:

def allpost(request):
    postData = Post.objects.all()
    paginator = Paginator(postData, 5)  # Show 5 contacts per page.

    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    return render(request, 'posts.html', {'page_obj': page_obj})




def CategoryView(request, cats='category__title'):
    category_posts = Post.objects.all()
    paginator = Paginator(category_posts, 5)  

    # We don't need to handle the case where the `page` parameter
    # is not an integer because our URL only accepts integers
    try:
        category_posts = paginator.page('cats')
    except EmptyPage:
        # if we exceed the page limit we return the last page
        category_posts = paginator.page(paginator.num_pages)

    return render(request, 'categories.html', {'category_posts': category_posts})

urls.py:

path('category/<str:cats>/', views.CategoryView, name ="category"),

categories.html

{% extends 'base.html' %}



{%block content%}

<h1> Category: {{ cats }} </h1>

{% for post in category_posts %}
   
<div class="container mt-3">
    <div class="row mb-2">
        <div class="col-md-6">
          <div class="card flex-md-row mb-4 box-shadow h-md-250">
            <div class="card-body d-flex flex-column align-items-start">
              <strong class="d-inline-block mb-2 text-primary">{{ post.category }}</strong>
              <h3 class="mb-0">
                <a class="text-dark" href="{% url 'detail' post.id %}">{{post.title}}</a>
              </h3>
              <div class="mb-1 text-muted">{{ post.public_date }}</div>
              <p class="card-text mb-auto">{{ post.summary }}</p>
              <a href="{% url 'detail' post.id %}">Continue reading</a>
            </div>
            <img class="card-img-right flex-auto d-none d-md-block" data-src="holder.js/200x250?theme=thumb" alt="Thumbnail [200x250]" style="width: 200px; height: 250px;" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22200%22%20height%3D%22250%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20200%20250%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_182c981dfc3%20text%20%7B%20fill%3A%23eceeef%3Bfont-weight%3Abold%3Bfont-family%3AArial%2C%20Helvetica%2C%20Open%20Sans%2C%20sans-serif%2C%20monospace%3Bfont-size%3A13pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_182c981dfc3%22%3E%3Crect%20width%3D%22200%22%20height%3D%22250%22%20fill%3D%22%2355595c%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%2256.20000076293945%22%20y%3D%22131%22%3EThumbnail%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E" data-holder-rendered="true">
          </div>
        </div>

    </div>
</div>

{% endfor %}

{% include 'pagination.html' %}

{%endblock%}

pagination.html

<div class="container mt-3">

<nav aria-label="Page navigation example">
  <ul class="pagination">

  {% if page_obj.has_previous %}

    <li class="page-item"><a class="page-link" href="/?page=1">First</a></li>
    <li class="page-item"><a class="page-link" href="/?page={{ page_obj.previous_page_number }}">Previous</a></li>

    {% endif %}


    <li class="page-item"><a class="current" href="/?page={{ page_obj.number }} of {{ page_obj.paginator.num_pages }}">1</a></li>



    {% if page_obj.has_next %}

    <li class="page-item"><a class="page-link" href="/?page={{ page_obj.next_page_number }}">Next</a></li>
    <li class="page-item"><a class="page-link" href="/?page={{lastpage}}">Last</a></li>

    {% endif %}

</div>

Я уже очень запутался в функции category views для использования параметра. поэтому я не использовал этот код в pagination.html.

===== в view.py ======

from django.core.paginator import Paginator

def HomeView(request):
    show_data = VehicleModel.objects.all() # Queryset For pagiantion
    # Pagination code start
    paginator = Paginator(show_data, 3, orphans=1)
    page_number = request.GET.get('page')
    show_data = paginator.get_page(page_number)
    # Pagination code end
    context = {'page_number':page_number}
    return render(request,'dashboard.html',context)

===== в html-файле ======

<!-- Pagination Block with page number  -->
<div class="container mt-5">
    <div class="row float-right ">
        <span class="m-0 p-0">

            {% if carset.has_previous %} # <!-- For Previous Button -->
            <a class="btn btn-outline-info" href="?page={{carset.previous_page_number}}&ok=#ok">Previous</a>
            {% endif %}


            <span>{% for pg in carset.paginator.page_range %} # <!-- For Page Numbers Buttons -->
                {% if carset.number == pg %}
                <a href="?page={{pg}}" class="btn btn-sm btn-primary">
                    <span class="badge">{{pg}}</span>
                </a>
                {% else %}
                <a href="?page={{pg}}" class="btn btn-sm btn-secondary">
                    <span class="badge">{{pg}}</span>
                </a>
                {% endif %}
                {% endfor %}</span>

            {% if carset.has_next %} # <!-- For Next Button -->
            <a class="btn btn-outline-info" href="?page={{carset.next_page_number}}&ok=#ok">Next</a>
            {% endif %}
        </span>
    </div>

</div>

=========== Ваш код выглядит следующим образом ============== здесь вы передали параметр category в функции, поэтому должны передать заголовок категории с вызывающим URL этой функции CategoryView(request, cats='category__title')

def CategoryView(request, cats='category__title'):
    category_posts = Post.objects.all()
    paginator = Paginator(category_posts, 5) 

с этим CategoryView(request, cats='category__title') вы хотите получить все посты ???

def

 CategoryView(request, cats='category__title'):
    category_posts = Post.objects.filter(cats__title = cats)
    paginator = Paginator(category_posts, 5)

ator = Paginator(category_posts, 5)

Вернуться на верх