聚学在线

http://coding.imooc.com/class/evaluation/368.html


  • 首页

  • 所有文章

  • 分类

  • 慕学在线网

  • 搜索
close

django pure pagination文档

发表于 2016-11-18   |   分类于 django , 慕学在线网   |     |   阅读次数

Introduction

The django app offers advanced pagination features without forcing major code changes within an existing project.

Django-pure-pagination is based upon Django’s core pagination module and is therefore compatible with the existing api.

Documentation for Django core pagination module <http://docs.djangoproject.com/en/dev/topics/pagination/>_

Features

  1. Uses same API as django.core.pagination and therefore is fully compatible with existing code.

  2. Has dynamic query string creation, which takes into consideration existing GET parameters.

  3. Out-of-the-box html rendering of the pagination

  4. Additional methods make it easier to render more advanced pagination templates.

Installation

Install package from PYPI:

::

pip install django-pure-pagination

or clone and install from repository:

::

git clone git@github.com:jamespacileo/django-pure-pagination.git
cd django-pure-pagination
python setup.py install

Add pure_pagination to INSTALLED_APPS

::

INSTALLED_APPS = (
    ...
    'pure_pagination',
)

Finally substitute from django.core.paginator import Paginator with from pure_pagination import Paginator

Settings

A few settings can be set within settings.py

::

PAGINATION_SETTINGS = {
    'PAGE_RANGE_DISPLAYED': 10,
    'MARGIN_PAGES_DISPLAYED': 2,

    'SHOW_FIRST_PAGE_WHEN_INVALID': True,
}

PAGE_RANGE_DISPLAYED is the number of pages neighbouring the current page which will be displayed (default is 10)

MARGIN_PAGES_DISPLAYED is the number of pages neighbouring the first and last page which will be displayed (default is 2)

Set SHOW_FIRST_PAGE_WHEN_INVALID to True when you want to just show first page when provided invalid page instead of 404 error

.. image:: http://i.imgur.com/LCqrt.gif

Usage example

Following is a simple example for function based views. For generic class-based views, see bellow.

view file: views.py

::

# views.py
from django.shortcuts import render_to_response

from pure_pagination import Paginator, EmptyPage, PageNotAnInteger


def index(request):

    try:
        page = request.GET.get('page', 1)
    except PageNotAnInteger:
        page = 1

    objects = ['john', 'edward', 'josh', 'frank']

    # Provide Paginator with the request object for complete querystring generation

    p = Paginator(objects, request=request)

    people = p.page(page)

    return render_to_response('index.html', {
        'people': people,
    }

template file: index.html

::

{% extends 'base.html' %}

{% block content %}

    {% for person in people.object_list %}
        
First name: {{ person }}
{% endfor %} {# The following renders the pagination html #}
{{ people.render }}
{% endblock %}

Usage

There a few different way you can make use of the features introduced within django-pure-pagination.

Easiest way to render the pagination is to call the render method i.e.

Alternatively you can access the Page object low level methods yourself

Special note: page_obj and current_page both point to the page object within the template.

::

{% load i18n %}
<div class="pagination">
    {% if page_obj.has_previous %}
            ‹‹ {% trans "previous" %}
        {% else %}
            ‹‹ {% trans "previous" %}
        {% endif %}
    {% for page in page_obj.pages %}
            {% if page %}
                {% ifequal page page_obj.number %}
                    {{ page }}
                {% else %}
                    {{ page }}
                {% endifequal %}
            {% else %}
                ...
            {% endif %}
        {% endfor %}
    {% if page_obj.has_next %}
            {% trans "next" %} ››
        {% else %}
            {% trans "next" %} ››
        {% endif %}
</div>

Generic Class-Based Views

Documentation for Django generic class-based views on https://docs.djangoproject.com/en/dev/ref/class-based-views/

view file:

  • views.py

    ::

    # views.py
    from django.views.generic import ListView
    
    from pure_pagination.mixins import PaginationMixin
    
    from my_app.models import MyModel
    
class MyModelListView(PaginationMixin, ListView):
    # Important, this tells the ListView class we are paginating
    paginate_by = 10

    # Replace it for your model or use the queryset attribute instead
    object = MyModel

template files:

Note that the Django generic-based list view will include the object page_obj in the context. More information on https://docs.djangoproject.com/en/dev/ref/generic-views/#list-detail-generic-views

  • _pagination.html

    ::

    {% load i18n %}
    <div class="pagination">
        {% if page_obj.has_previous %}
                    ‹‹ {% trans "previous" %}
                {% else %}
                    ‹‹ {% trans "previous" %}
                {% endif %}
        {% for page in page_obj.pages %}
                    {% if page %}
                        {% ifequal page page_obj.number %}
                            {{ page }}
                        {% else %}
                            {{ page }}
                        {% endifequal %}
                    {% else %}
                        ...
                    {% endif %}
                {% endfor %}
        {% if page_obj.has_next %}
                    {% trans "next" %} ››
                {% else %}
                    {% trans "next" %} ››
                {% endif %}
    </div>
    
  • my_app/myobject_list.html

    ::

{% extends 'base.html' %}

{% block content %}

        {% for object in object_list %}
            
First name: {{ object.first_name }}
{% endfor %} {# The following renders the pagination html #} {% include "_pagination.html" %} {% endblock %}

django 实战课程资源下载

发表于 2016-11-18   |   分类于 django , 慕学在线网   |     |   阅读次数
  1. xadmin

    点击下载

  2. django simple captcha

    文档地址

  3. django pure pagination

    下载地址

    文档地址

django介绍

发表于 2016-10-15   |     |   阅读次数

html

发表于 2016-10-14   |     |   阅读次数
  1. html元素类型
在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素、内联元素(又叫行内元素)和内联块状元素。

常用的块状元素有:

    <div>、<p>、<h1>...<h6>、<ol>、<ul>、<dl>、<table>、<address>、<blockquote> 、<form>

常用的内联元素有:

    <a>、<span>、<br>、<i>、<em>、<strong>、<label>、<q>、<var>、<cite>、<code>

常用的内联块状元素有:

    <img>、<input>
  1. line-height深入理解

    http://www.jb51.net/css/66785.html

  2. 深入理解float

    http://www.cnblogs.com/caseast/p/5831240.html

12
bobbyli

bobbyli

http://coding.imooc.com/class/evaluation/368.html

14 日志
3 分类
9 标签
RSS
GitHub 微博
Links
  • 聚学在线
© 2022 bobbyli 蜀ICP备16029565号-1