Answer the question
In order to leave comments, you need to log in
How to make pagination work properly when using django-filters?
It was necessary to implement a filter on the site for records. Before that, pagination for posts was also implemented. I figured out the filter and did it, the records are perfectly filtered, but when switching to another page, the filter was not saved. I decided to unload all data from the GET request in the link to the next page, and now the filtering was saved when moving to the second page.
Here is the code from Views
class MainPage(ListView):
model = DailyOrders
template_name = 'Catalog/main_page.html'
context_object_name = 'orders'
paginate_by = 3
allow_empty = True
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = ClientsForm()
context['filter'] = DailyOrdersFilter(self.request.GET, queryset = DailyOrders.objects.all())
return context
def get_queryset(self):
qs = DailyOrders.objects.all()
word = DailyOrdersFilter(self.request.GET, queryset=qs)
return word.qs
{% for order in orders%}
<tr>
<td>{{order.data|date:"d.m.Y"}}</td>
<td>{{order.city}}</td>
<td>{{order.name_of_company}}</td>
<td>{{order.kind_of_activity}}</td>
<td>{{order.details}}</td>
<td>{{order.square}}</td>
<td>{{order.floor}}</td>
<td>{{order.requirements}}</td>
<td>{{order.district}}</td>
<td>{{order.budget}}</td>
</tr>
{% endfor %}
</table>
</div>
{% if page_obj.has_other_pages %}
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center my-3">
{% if page_obj.has_previous %}
<li class="page-item ">
<a class="page-link text-dark" href="?page={{page_obj.previous_page_number}}{% if request.GET%}&{{ request.GET.urlencode }}{% endif %}"
tabindex="-1">Пердыдущая</a>
</li>
{% endif %}
{% for p in page_obj.paginator.page_range %}
{% if page_obj.number == p %}
<li class="page-item active" aria-current="page">
<a class="page-link text-dark bg-danger border-dark" href="?page={{p}}{% if request.GET%}&{{ request.GET.urlencode }}{% endif %}">{{p}}</a>
</li>
{% elif p > page_obj.number|add:-3 and p < page_obj.number|add:3 %}
<li class="page-item"><a class="page-link text-dark " href="?page={{p}}{% if request.GET %}&{{ request.GET.urlencode}}{% endif %}">{{p}}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link text-dark " href="?page={{page_obj.next_page_number}}{% if request.GET%}&{{ request.GET.urlencode }}{% endif %}"
tabindex="-1">Следующая</a>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
http://127.0.0.1:8000/?page=3&page=2&city=Казань&square_for_filter_min=&square_for_filter_max=&floor=&district=&budget_for_filter_min=&budget_for_filter_max=
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question