Django пагинация не работает должным образом при использовании метода POST в форме поиска
Я пытаюсь найти элемент в форме, используя метод POST, и я получил результаты. Но когда я использую пагинацию Django, я получаю результаты на первой странице. Когда я нажимаю на кнопку next или 2 в Django paginator, я получаю ошибку, подобную этой.
The view profiles.views.individual_home didn't return an HttpResponse object. It returned None instead.
Здесь форма поиска находится на одной html странице, а результаты отображаются на другой html странице.
views.py
def individual_home(request):
if request.method == 'POST':
keyword = request.POST.get('keywords')
print(keyword)
location = request.POST.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:
Идея заключается в том, чтобы передать ключевое слово и местоположение в контекст.
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")
измените падигнацию следующим образом.
<!--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 }}">…</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 }}">…</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-->