Рефакторинг пагинации через custom template tag url_replace

- Создан элегантный тег для автоматического сохранения GET-параметров
- Код пагинации сократился в 10 раз
- Переиспользуется в любых шаблонах проекта
- 100 процентов Django-way без хаков
This commit is contained in:
2026-01-03 15:19:47 +03:00
parent f1f44a93b2
commit 36cca23b60
3 changed files with 39 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
{% extends "base.html" %}
{% load query_tags %}
{% block title %}Клиенты{% endblock %}
@@ -132,12 +133,12 @@
<ul class="pagination pagination-sm justify-content-center mb-0">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?page=1{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.has_notes %}&has_notes=on{% endif %}{% if request.GET.no_phone %}&no_phone=on{% endif %}{% if request.GET.no_email %}&no_email=on{% endif %}" title="Первая страница">
<a class="page-link" href="{% url_replace page=1 %}" title="Первая страница">
&laquo;&laquo;
</a>
</li>
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.has_notes %}&has_notes=on{% endif %}{% if request.GET.no_phone %}&no_phone=on{% endif %}{% if request.GET.no_email %}&no_email=on{% endif %}" title="Предыдущая">
<a class="page-link" href="{% url_replace page=page_obj.previous_page_number %}" title="Предыдущая">
&laquo;
</a>
</li>
@@ -160,7 +161,7 @@
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="?page={{ num }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.has_notes %}&has_notes=on{% endif %}{% if request.GET.no_phone %}&no_phone=on{% endif %}{% if request.GET.no_email %}&no_email=on{% endif %}">{{ num }}</a>
<a class="page-link" href="{% url_replace page=num %}">{{ num }}</a>
</li>
{% endif %}
{% endif %}
@@ -169,12 +170,12 @@
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.next_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.has_notes %}&has_notes=on{% endif %}{% if request.GET.no_phone %}&no_phone=on{% endif %}{% if request.GET.no_email %}&no_email=on{% endif %}" title="Следующая">
<a class="page-link" href="{% url_replace page=page_obj.next_page_number %}" title="Следующая">
&raquo;
</a>
</li>
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.has_notes %}&has_notes=on{% endif %}{% if request.GET.no_phone %}&no_phone=on{% endif %}{% if request.GET.no_email %}&no_email=on{% endif %}" title="Последняя страница">
<a class="page-link" href="{% url_replace page=page_obj.paginator.num_pages %}" title="Последняя страница">
&raquo;&raquo;
</a>
</li>