fix pagination

This commit is contained in:
Cayo Puigdefabregas 2024-10-02 12:51:40 +02:00
parent 2b4b469b0e
commit 2f17d01780
5 changed files with 64 additions and 12 deletions

View file

@ -84,14 +84,18 @@ class InventaryMixin(DashboardView, TemplateView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
limit = self.request.GET.get("limit", 10)
page = self.request.GET.get("page", 1)
if limit:
try:
limit = int(limit)
page = int(page)
except:
raise Http404
limit = self.request.GET.get("limit")
page = self.request.GET.get("page")
try:
limit = int(limit)
page = int(page)
if page < 1:
page = 1
if limit < 1:
limit = 10
except:
limit = 10
page = 1
offset = (page - 1) * limit
devices, count = self.get_devices(self.request.user, offset, limit)

View file

@ -0,0 +1,47 @@
{% load i18n %}
{% load range %}
<ul class="pagination">
{% if page_number > 1 %}
<li class="previous">
<a type="button" class="btn btn-grey border border-dark" href="?page=1&limit={{ limit }}">
&laquo;
</a>
</li>
<li class="previous">
<a type="button" class="btn btn-grey border border-dark" href="?page={{ page_number|add:-1 }}&limit={{ limit }}">
{% trans 'Previous' %}
</a>
</li>
{% endif %}
{% if total_pages > 1 %}
{% for p in total_pages|range_filter:page_number %}
<li {% if p == page_number %}class="active"{% endif %}>
<a type="button" class="btn btn-grey{% if p == page_number %}-selected{% endif %}
border border-dark"
{% if p == page_number or p == "..." %}
href="#">
{% else %}
href="?page={{ p }}&limit={{ limit }}">
{% endif %}
{{ p }}
</a>
</li>
{% endfor %}
{% endif %}
{% if page_number < total_pages %}
<li class="previous">
<a type="button" class="btn btn-grey border border-dark" href="?page={{ page_number|add:+1 }}&limit={{ limit }}">
{% trans 'Next' %}
</a>
</li>
<li class="previous">
<a type="button" class="btn btn-grey border border-dark" href="?page={{ total_pages }}&limit={{ limit }}">
&raquo;
</a>
</li>
{% endif %}
</ul>

View file

@ -54,12 +54,12 @@
</tbody>
{% endfor %}
</table>
<button type="submit" value="{% url 'lot:del_devices' %}" name="url">Remove</button> <button type="submit" name="url" value="{% url 'lot:add_devices' %}">add</button>
<button class="btn btn-green-admin" type="submit" value="{% url 'lot:del_devices' %}" name="url">Remove</button> <button class="btn btn-green-admin" type="submit" name="url" value="{% url 'lot:add_devices' %}">add</button>
</form>
</div>
<div class="row mt-3">
<div class="col">
{% render_pagination page total_pages %}
{% render_pagination page total_pages limit %}
</div>
</div>
{% endblock %}

View file

@ -3,7 +3,7 @@ from django import template
register = template.Library()
@register.inclusion_tag('pagination.html')
def render_pagination(page_number, total_pages):
def render_pagination(page_number, total_pages, limit=10):
"""
Template tag for render pagination
@ -16,4 +16,5 @@ def render_pagination(page_number, total_pages):
return {
'page_number': page_number,
'total_pages': total_pages,
'limit': limit
}

View file

@ -26,7 +26,7 @@
{% endfor %}
{% endfor %}
<button type="submit">Save</button>
<button class="btn btn-green-admin" type="submit">Save</button>
</form>
{% endblock %}