pagination #6
|
@ -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)
|
||||
|
|
47
dashboard/templates/pagination.html
Normal file
47
dashboard/templates/pagination.html
Normal 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 }}">
|
||||
«
|
||||
</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 }}">
|
||||
»
|
||||
</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
</ul>
|
|
@ -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 %}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
{% endfor %}
|
||||
{% endfor %}
|
||||
|
||||
<button type="submit">Save</button>
|
||||
<button class="btn btn-green-admin" type="submit">Save</button>
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
|
|
Loading…
Reference in a new issue