Separating pagination from context on model. Introduce pagination on template. Works on #57

pull/71/head
Edd Baldry 2017-02-24 22:03:50 +00:00
rodzic b9f4661b0f
commit e96591d5f9
3 zmienionych plików z 88 dodań i 42 usunięć

Wyświetl plik

@ -94,26 +94,27 @@ class BreadsIndexPage(BasePageFieldsMixin, Page):
subpage_types = ['BreadPage'] subpage_types = ['BreadPage']
@property
def breads(self):
return BreadPage.objects.live().descendant_of(
self).order_by('-first_published_at')
def paginate(self, request, *args):
page = request.GET.get('page')
paginator = Paginator(self.breads, 2)
try:
pages = paginator.page(page)
except PageNotAnInteger:
pages = paginator.page(1)
except EmptyPage:
pages = paginator.page(paginator.num_pages)
return pages
def get_context(self, request): def get_context(self, request):
context = super(BreadsIndexPage, self).get_context(request) context = super(BreadsIndexPage, self).get_context(request)
# Get the full unpaginated listing of resource pages as a queryset - breads = self.paginate(request, self.breads)
# replace this with your own query as appropriate
all_resources = self.get_children().live()
paginator = Paginator(all_resources, 5) # Show 5 resources per page context['breads'] = breads
page = request.GET.get('page')
try:
resources = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
resources = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
resources = paginator.page(paginator.num_pages)
# make the variable 'resources' available on the template
context['resources'] = resources
context['paginator'] = paginator
return context return context

Wyświetl plik

@ -35,29 +35,41 @@
</div> </div>
</div> </div>
{% if resources.has_other_pages %}
<div class="clearfix"> {% if breads.paginator.count > 1 %}
<div class="pagination-wrapper"> <div class="container">
<ul class="pagination"> <div class="row">
{% if resources.has_previous %} <div class="col-md-12">
<li class="arrows"><a href="?q={{ query_string|urlencode }}&amp;page={{ resources.previous_page_number }}" class="arrow-left"></a></li> {% include "includes/pagination.html" with subpages=breads %}
{% else %} </div>
<li class="arrows disabled"><span class="arrow-left"></span></li> </div>
{% endif %} </div>
{% for i in resources.paginator.page_range %} {% endif %}
{% if resources.number == i %} {% comment %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li> {% if resources.has_other_pages %}
{% else %} <div class="clearfix">
<li><a href="?q={{ query_string|urlencode }}&amp;page={{ i }}">{{ i }}</a></li> <div class="pagination-wrapper">
{% endif %} <ul class="pagination">
{% endfor %} {% if resources.has_previous %}
{% if resources.has_next %} <li class="arrows"><a href="?q={{ query_string|urlencode }}&amp;page={{ resources.previous_page_number }}" class="arrow-left"></a></li>
<li class="arrows"><a href="?q={{ query_string|urlencode }}&amp;page={{ resources.next_page_number }}"><i class="arrow-right"></i></a></li> {% else %}
{% else %} <li class="arrows disabled"><span class="arrow-left"></span></li>
<li class="arrows disabled"><span><i class="arrow-right"></i></span></li> {% endif %}
{% endif %} {% for i in resources.paginator.page_range %}
</ul> {% if resources.number == i %}
</div> <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
</div> {% else %}
{% endif %} <li><a href="?q={{ query_string|urlencode }}&amp;page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if resources.has_next %}
<li class="arrows"><a href="?q={{ query_string|urlencode }}&amp;page={{ resources.next_page_number }}"><i class="arrow-right"></i></a></li>
{% else %}
<li class="arrows disabled"><span><i class="arrow-right"></i></span></li>
{% endif %}
</ul>
</div>
</div>
{% endif %}
{% endcomment %}
{% endblock content %} {% endblock content %}

Wyświetl plik

@ -0,0 +1,33 @@
{% load navigation_tags %}
<nav role="navigation" aria-label="Pagination">
<ul class="pagination">
{% if subpages.has_previous %}
<li class="page-item">
<a href="?page={{ subpages.previous_page_number }}" class="page-link previous arrows">previous</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">previous</a>
</li>
{% endif %}
{% for i in subpages.paginator.page_range %}
{% if subpages.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li class="page-item"><a href="?page={{ query_string|urlencode }}&amp;page={{ i }}" class="page-link">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if subpages.has_next %}
<li class="page-item">
<a href="?page={{ subpages.next_page_number }}" class="page-link next arrows">next</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link">next</a>
</li>
{% endif %}
</ul>
</nav>