[Answered ]-Django pagination is not working properly when using POST method in Search form

1👍

The idea is to pass the keyword and the location to the context.

def individual_home(request):
if request.method == 'GET':
    keyword = request.GET.get('keywords')
    print(keyword)
    location = request.GET.get('location')
    print(location)
    ind = IndividualProfile.objects.get(user_id=request.user.id)

    details = companyjob.objects.filter(
        Q(job_title=keyword) | Q(qualification=keyword) | Q(skills_required=keyword) | Q(job_location=location) & Q(closing_date__gte=datetime.date.today()))
    if details:
        print(details)
        count = details.count()
        paginator = Paginator(details, 3)
        page_number = request.GET.get('page')
        page_obj = paginator.get_page(page_number)

        return render(request, "individual_home.html",
                      {'keyword':keyword,'location':location,'jobs': page_obj,'count': count, 'ind': ind})
    else:
        return redirect("individual_homes")

change the padignation like this.

        <!--Pagination-->

            <nav aria-label="Page navigation example">
                <ul class="pagination justify-content-center">
                {% if jobs.has_previous %}
                    <li class="page-item">
                    <a class="page-link" href="?page={{ jobs.previous_page_number }}&keywords={{ keyword }}&location={{ location }}">Previous</a>
                  </li>
                {% else %}
                    <li class="page-item disabled">
                    <a class="page-link" href="#" tabindex="-1" aria-disabled="True">Previous</a>
                  </li>
                {% endif %}

                {% if jobs.number|add:'-4' > 1 %}
                    <li class="page-item"><a class="page-link" href="?page={{ jobs.previous_page_number }}&keywords={{ keyword }}&location={{ location }}">&hellip;</a></li>
                {% endif %}

                {% for i in jobs.paginator.page_range %}
                    {% if jobs.number == i %}
                        <li class="page-item active" aria-current="page">
                      <span class="page-link">
                        {{ i }}
                        <span class="sr-only">(current)</span>
                      </span>
                    </li>
                    {% elif i > jobs.number|add:'-5' and i < jobs.number|add:'5' %}
                         <li class="page-item"><a class="page-link" href="?page={{ i }}&keywords={{ keyword }}&location={{ location }}"">{{ i }}</a></li>
                    {% endif %}
                {% endfor %}

                {% if jobs.paginator.num_pages > jobs.number|add:'4' %}
                   <li class="page-item"><a class="page-link" href="?page={{ jobs.number|add:'5' }}&keywords={{ keyword }}&location={{ location }}">&hellip;</a></li>
                {% endif %}

                {% if jobs.has_next %}
                    <li class="page-item">
                    <a class="page-link" href="?page={{ jobs.next_page_number }}&keywords={{ keyword }}&location={{ location }}">Next</a>
                  </li>
                {% else %}
                    <li class="page-item disabled">
                    <a class="page-link" href="#" tabindex="-1" aria-disabled="True">Next</a>
                  </li>
                {% endif %}
              </ul>
            </nav>

        <!--end of Pagination-->

Leave a comment