django-pagination的不同模板

时间:2012-09-26 13:21:49

标签: django django-templates django-pagination

我刚刚开始使用django-pagination,它包含在我用于其中一个项目的其他第三方应用中。

是否有人知道是否可以使用自定义版本替换django-pagination中的 pagination.html 模板而无需破解实际的应用程序?文档中没有提及,pagaination.html在templatetag(paginate())中被硬编码。我想知道是否有一种机制允许覆盖通过

设置的模板
register.inclusion_tag('pagination/pagination.html', takes_context=True)(
paginate)

来自我自己的应用程序?

3 个答案:

答案 0 :(得分:5)

您只需在项目的pagination/pagination.html文件夹中创建template,它就会优先于分页应用pagination.html。因此,只需将分页应用程序版本中的代码复制并粘贴到您的版本中,然后编辑您的内容

您提到的模板标签只是针对模板呈现上下文,因此您无需对其进行任何黑客攻击即可更改布局/外观/模板。

答案 1 :(得分:1)

主要分页使用你的模板“页面设计”,默认的django模板不需要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})

在模板list.html中,您需要在页面之间包含导航以及来自对象本身的任何有趣信息:

{% 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/

答案 2 :(得分:0)

如果你还在寻找这个,那就是我用的。

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}

在上下文中传递make_pagination的输出作为&#39;分页&#39;然后

<p class="_nav">
{% for page in pagination.pages %}
  <a class="{{ page.class }}" href="{{ page.val }}/">{{ page.text }}</a>
{% endfor %}
</p>