Home > front end >  Django pagination is not working properly when using POST method in Search form
Django pagination is not working properly when using POST method in Search form

Time:01-11

I am trying to search an item in a form using POST method and I got the results. But when I use Django pagination, I got the results in the first page. When I click the next or 2 button in Django paginator I got an error like this.

The view profiles.views.individual_home didn't return an HttpResponse object. It returned None instead.

Here search form is in one html page and results shown in another html page.

views.py

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",
                      {'jobs': page_obj,'count': count, 'ind': ind})
    else:
        return redirect("individual_homes")

individual_home.html:

<div >
        <div >
            <div >
                <div >
                    <form method="GET" action="{% url 'individual_home' %}"  style="background-color:#6351ce;">
                        
                        <div >
                            <div >
                                <div >
                                    <input type="text"  placeholder="Enter Your Keywords" id="keywords" name="keywords">
                                </div>
                            </div>
                            <div >
                                <input type="text"  placeholder="Location" id="location" name="location">


                            <div >
                                <button type="submit"  id="contact-submit">
                                    Search
                                </button>
                            </div>
                        </div>
                    </form>

                    <div >
                        <p >Job Openings Based On Your Profile : {{count}}</p>
                        {% for i in jobs %}
                            <div >
                                <div >
                                    <a href="{% url 'job_details' i.id %}">
                                    <div  style="background-color:#6351ce;">
                                        {{i.job_title.0}}
                                    </div></a>
                                    <div >
                                        <a href="{% url 'job_details' i.id %}"><h4 >{{i.job_title}}</h4></a><br>
                                        <p ><span style="width:10px;height:10px;border-radius:50%;background-color:green;display:inline-block;"></span>&nbsp&nbspActive</p>
                                        <ul >
                                            <li >
                                                <i ></i> {{i.user.first_name}} {{i.user.last_name}}
                                            </li>
                                            <li >
                                                <i ></i> {{i.job_location}}
                                            </li>
                                            <li >
                                                <i ></i> {{i.salary}} {{i.salary_period}}
                                            </li>
                                            <li >
                                                <i ></i> {{ i.posted_date | naturaltime }}
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                                <div >
                                    <a href="{% url 'job_details' i.id %}"  style="outline-color:#6351ce;">{{i.type}}</a>

                                </div>
                            </div>
                        {% endfor %}
                    </div>
                </div>


                <!--Pagination-->

                    <nav aria-label="Page navigation example">
                        <ul >
                        {% if jobs.has_previous %}
                            <li >
                            <a  href="?page={{ jobs.previous_page_number }}">Previous</a>
                          </li>
                        {% else %}
                            <li >
                            <a  href="#" tabindex="-1" aria-disabled="True">Previous</a>
                          </li>
                        {% endif %}

                        {% if jobs.number|add:'-4' > 1 %}
                            <li ><a  href="?page={{ jobs.number|add:'-5' }}">&hellip;</a></li>
                        {% endif %}

                        {% for i in jobs.paginator.page_range %}
                            {% if jobs.number == i %}
                                <li  aria-current="page">
                              <span >
                                {{ i }}
                                <span >(current)</span>
                              </span>
                            </li>
                            {% elif i > jobs.number|add:'-5' and i < jobs.number|add:'5' %}
                                 <li ><a  href="?page={{ i }}">{{ i }}</a></li>
                            {% endif %}
                        {% endfor %}

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

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

                <!--end of Pagination-->

            </div>
        </div>


CodePudding user response:

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 >
                {% if jobs.has_previous %}
                    <li >
                    <a  href="?page={{ jobs.previous_page_number }}&keywords={{ keyword }}&location={{ location }}">Previous</a>
                  </li>
                {% else %}
                    <li >
                    <a  href="#" tabindex="-1" aria-disabled="True">Previous</a>
                  </li>
                {% endif %}

                {% if jobs.number|add:'-4' > 1 %}
                    <li ><a  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  aria-current="page">
                      <span >
                        {{ i }}
                        <span >(current)</span>
                      </span>
                    </li>
                    {% elif i > jobs.number|add:'-5' and i < jobs.number|add:'5' %}
                         <li ><a  href="?page={{ i }}&keywords={{ keyword }}&location={{ location }}"">{{ i }}</a></li>
                    {% endif %}
                {% endfor %}

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

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

        <!--end of Pagination-->
  •  Tags:  
  • Related