Question

I have just started playing around with django-pagination, which is included in some other 3rd-party app I am using for one of my projects.

Does anyone know if it's possible to replace the pagination.html template inside django-pagination with a custom version without having to hack the actual app? There is nothing mentioned in the docs and pagaination.html is hardcoded inside the templatetag (paginate()). I was wondering if there is a mechanism that allows overriding the template that was set via

register.inclusion_tag('pagination/pagination.html', takes_context=True)(
paginate)

from within my own app?

Était-ce utile?

La solution

You can simply create pagination/pagination.html within the template folder of your project and it will take precedence over the pagination app's pagination.html. So simply copy and paste the code from the pagination app's version into your version and edit to your hearts content

The template tag you mention simply renders a context against the template, so you don't need to do any hacking with it to change the layout/appearance/template.

Autres conseils

mainly pagination use your template " page design ", the default django template don't need url for templates

from django.core.paginator import Paginator, InvalidPage, EmptyPage

def listing(request):
    contact_list = Contacts.objects.all()
    paginator = Paginator(contact_list, 25) # Show 25 contacts per page

    # Make sure page request is an int. If not, deliver first page.
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1

    # If page request (9999) is out of range, deliver last page of results.
    try:
        contacts = paginator.page(page)
    except (EmptyPage, InvalidPage):
        contacts = paginator.page(paginator.num_pages)

    return render_to_response('list.html', {"contacts": contacts})

In the template list.html, you'll want to include navigation between pages along with any interesting information from the objects themselves:

{% for contact in contacts.object_list %}
    {# Each "contact" is a Contact model object. #}
    {{ contact.full_name|upper }}<br />
    ...
{% endfor %}

<div class="pagination">
    <span class="step-links">
        {% if contacts.has_previous %}
            <a href="?page={{ contacts.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
        </span>

        {% if contacts.has_next %}
            <a href="?page={{ contacts.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

https://docs.djangoproject.com/en/1.3/topics/pagination/

If you are still looking for this here is what i used.

def make_pagination(paginator, items=4, show_arrows=True, edges=2):
    """
    Returns a list by which a pagination can be prepared in template by simply
    iterating over it

    :param paginator: the django paginated queryset. (what you get after paginator.page(.....))
    :param items: the number of items to show in center sub-list (place even numbers only)
    :param show_arrows: show arrows at end and beginning.
    :param edges: no of items to show at the edges including '...' (set edges=0 to hide )
    :return: (sample output)
    {'current_page': 6,
     'total_pages': 155,
     'pages': [{'text': u'\xab', 'class': 'first', 'val': 1}, {'text': 1, 'val': 1}, {'text': 2, 'val': 2}, {'text': '...', 'val': ''}, {'text': 4, 'val': 4}, {'text': 5, 'val': 5}, {'text': 6, 'class': 'act', 'val': 6}, {'text': 7, 'val': 7}, {'text': 8, 'val': 8}, {'text': '...', 'val': ''}, {'text': 154, 'val': 154}, {'text': 155, 'val': 155}, {'text': u'\xbb', 'class': 'last', 'val': 155}]
     }
    """

    find = paginator.number
    span = items / 2
    span = span if items % 2 == 0 else (span + 1)
    page_range = paginator.paginator.page_range
    indx = page_range.index(find) if find in page_range else 0
    L = indx - span
    R = indx + span + 1
    _len = len(page_range)

    if L < 0:
        R += 0 - L
        L = 0

    if R - _len > 0:
        L -= R - _len
        R = _len

    sel_range = page_range[L:R]

    if edges:
        Ls = page_range[:edges]
        if Ls[-1] < sel_range[0]:
            sel_range = Ls + ['...'] + sel_range
        else:
            sel_range = list(set(Ls + sel_range))

        Rs = page_range[-edges:]
        if sel_range[-1] < Rs[0]:
            sel_range = sel_range + ['...'] + Rs
        else:
            sel_range = list(set(sel_range + Rs))

    pages = []
    for item in sel_range:
        pg = {'text': item, 'val': item}
        if item == '...':
            pg['val'] = ''
        if item == find:
            pg['class'] = 'act'
        pages.append(pg)

    if show_arrows:
        if page_range[0] == find:
            pages.insert(0, {'text': u'«', 'class': 'first act', 'val': 1})
        else:
            pages.insert(0, {'text': u'«', 'class': 'first', 'val': 1})

        if page_range[-1] == find:
            pages.append({'text': u'»', 'class': 'last act', 'val': page_range[-1]})
        else:
            pages.append({'text': u'»', 'class': 'last', 'val': page_range[-1]})

    return {'current_page': find, 'total_pages': paginator.paginator.num_pages, 'pages': pages}

pass the output of make_pagination in context as 'pagination' and then

<p class="_nav">
{% for page in pagination.pages %}
  <a class="{{ page.class }}" href="{{ page.val }}/">{{ page.text }}</a>
{% endfor %}
</p>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top