pagination helpers
This commit is contained in:
Родитель
6345dadcb1
Коммит
15d2bb3c87
|
@ -1,5 +1,36 @@
|
|||
"""
|
||||
Miscellaneous helpers that make Django compatible with AMO.
|
||||
"""
|
||||
import functools
|
||||
|
||||
from django.core import paginator
|
||||
|
||||
from .models import LegacyModel, TranslatedField
|
||||
|
||||
|
||||
def paginate(request, queryset, per_page=20):
|
||||
"""Get a Paginator, abstracting some common paging actions."""
|
||||
p = paginator.Paginator(queryset, per_page)
|
||||
|
||||
# Get the page from the request, make sure it's an int.
|
||||
try:
|
||||
page = int(request.GET.get('page', 1))
|
||||
except ValueError:
|
||||
page = 1
|
||||
|
||||
# Get a page of results, or the first page if there's a problem.
|
||||
try:
|
||||
paginated = p.page(page)
|
||||
except (paginator.EmptyPage, paginator.InvalidPage):
|
||||
paginated = p.page(1)
|
||||
|
||||
paginated.add_query = functools.partial(add_query, request)
|
||||
return paginated
|
||||
|
||||
|
||||
def add_query(request, **kwargs):
|
||||
"""Return absolute url to current page with ``kwargs`` added to GET."""
|
||||
base = request.build_absolute_uri(request.path)
|
||||
query = dict(request.GET)
|
||||
query.update(kwargs)
|
||||
return '%s?%s' % (base, '&'.join('%s=%s' % q for q in query.items()))
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
{% if num_pages > 1 %}
|
||||
<ol class="pagination">
|
||||
{% for x in range(1, num_pages) %}
|
||||
<li {{ x|class_selected(pager.number) }}>
|
||||
<a href="{{ pager.add_query(page=x) }}">{{ x }}</a>
|
||||
</li>
|
||||
{% endfor %}
|
||||
<li rel="next">
|
||||
<a href="#page={{ pager.next_page_number() }}">
|
||||
{{ pager.next_page_number() }}
|
||||
</a>
|
||||
</li>
|
||||
</ol>
|
||||
<div class="num-results">
|
||||
{% trans begin=pager.start_index(), end=pager.end_index() %}
|
||||
Results <strong>{{ begin }}</strong>–<strong>{{ end }}</strong>
|
||||
of {{ count }}
|
||||
{% endtrans %}
|
||||
</div>
|
||||
{% endif %}
|
|
@ -0,0 +1,11 @@
|
|||
import jinja2
|
||||
|
||||
from jingo import register, env
|
||||
|
||||
|
||||
@register.filter
|
||||
def paginator(pager):
|
||||
c = {'pager': pager, 'num_pages': pager.paginator.num_pages,
|
||||
'count': pager.paginator.count}
|
||||
t = env.get_template('amo/paginator.html').render(**c)
|
||||
return jinja2.Markup(t)
|
Загрузка…
Ссылка в новой задаче