1

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 class="container mt-5">
        <div class="row mt-5">
            <div class="col-lg-10 mx-auto">
                <div class="career-search mb-60">
                    <form method="GET" action="{% url 'individual_home' %}" class="career-form mb-60" style="background-color:#6351ce;">
                        
                        <div class="row">
                            <div class="col-md-6 col-lg-4 my-3">
                                <div class="input-group position-relative">
                                    <input type="text" class="form-control" placeholder="Enter Your Keywords" id="keywords" name="keywords">
                                </div>
                            </div>
                            <div class="col-md-6 col-lg-4 my-3">
                                <input type="text" class="form-control" placeholder="Location" id="location" name="location">


                            <div class="col-md-6 col-lg-4 my-3">
                                <button type="submit" class="btn btn-lg btn-block btn-light btn-custom" id="contact-submit">
                                    Search
                                </button>
                            </div>
                        </div>
                    </form>

                    <div class="filter-result">
                        <p class="mb-30 ff-montserrat">Job Openings Based On Your Profile : {{count}}</p>
                        {% for i in jobs %}
                            <div class="job-box d-md-flex align-items-center justify-content-between mb-30">
                                <div class="job-left my-4 d-md-flex align-items-center flex-wrap">
                                    <a href="{% url 'job_details' i.id %}">
                                    <div class="img-holder mr-md-4 mb-md-0 mb-4 mx-auto mx-md-0 d-md-none d-lg-flex" style="background-color:#6351ce;">
                                        {{i.job_title.0}}
                                    </div></a>
                                    <div class="job-content">
                                        <a href="{% url 'job_details' i.id %}"><h4 class="text-center text-md-left">{{i.job_title}}</h4></a><br>
                                        <p class="mr-md-4 text-success"><span style="width:10px;height:10px;border-radius:50%;background-color:green;display:inline-block;"></span>&nbsp&nbspActive</p>
                                        <ul class="d-md-flex flex-wrap text-capitalize ff-open-sans">
                                            <li class="mr-md-4">
                                                <i class="fa fa-briefcase mr-2"></i> {{i.user.first_name}} {{i.user.last_name}}
                                            </li>
                                            <li class="mr-md-4">
                                                <i class="zmdi zmdi-pin mr-2"></i> {{i.job_location}}
                                            </li>
                                            <li class="mr-md-4">
                                                <i class="fa fa-inr mr-2"></i> {{i.salary}} {{i.salary_period}}
                                            </li>
                                            <li class="mr-md-4">
                                                <i class="zmdi zmdi-time mr-2"></i> {{ i.posted_date | naturaltime }}
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                                <div class="job-right my-4 flex-shrink-0">
                                    <a href="{% url 'job_details' i.id %}" class="btn btn-outline-danger" style="outline-color:#6351ce;">{{i.type}}</a>

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


                <!--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 }}">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.number|add:'-5' }}">&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 }}">{{ 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' }}">&hellip;</a></li>
                        {% endif %}

                        {% if jobs.has_next %}
                            <li class="page-item">
                            <a class="page-link" href="?page={{ jobs.next_page_number }}">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-->

            </div>
        </div>


Abin Benny
  • 125
  • 2
  • 10
  • 1
    it is better not to use `POST` for searching ... try to use `GET` instead. – Thierno Amadou Sow Jan 08 '22 at 06:58
  • When I use 'GET' instead of 'POST' error is not showing when next button in the Django paginator is clicked. But pagination is not working. When next button is clicked it did not redirecting to the next page or page in which button is clicked.@Thierno Amadou Sow – Abin Benny Jan 08 '22 at 07:42
  • could you please update your question with the `GET` ? so that i can see what you have changed. – Thierno Amadou Sow Jan 08 '22 at 10:20
  • I have edited my question with GET. Could you please go through it and suggest a solution@Thierno Amadou Sow – Abin Benny Jan 10 '22 at 03:50

1 Answers1

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-->
Thierno Amadou Sow
  • 2,523
  • 2
  • 5
  • 19