Anonymous avatar Anonymous committed f7bf1c6

Added digg-style pagination.

Comments (0)

Files changed (12)

 <div class="contents topic" id="index">
 <p class="topic-title first">Index</p>
 <ul class="auto-toc simple">
-<li><a class="reference internal" href="#introduction" id="id1">1&nbsp;&nbsp;&nbsp;Introduction</a></li>
-<li><a class="reference internal" href="#installation" id="id2">2&nbsp;&nbsp;&nbsp;Installation</a><ul class="auto-toc">
-<li><a class="reference internal" href="#requirements" id="id3">2.1&nbsp;&nbsp;&nbsp;Requirements</a></li>
+<li><a class="reference internal" href="#introduction" id="id1">1   Introduction</a></li>
+<li><a class="reference internal" href="#installation" id="id2">2   Installation</a><ul class="auto-toc">
+<li><a class="reference internal" href="#requirements" id="id3">2.1   Requirements</a></li>
 </ul>
 </li>
-<li><a class="reference internal" href="#usage" id="id4">3&nbsp;&nbsp;&nbsp;Usage</a><ul class="auto-toc">
-<li><a class="reference internal" href="#settings" id="id5">3.1&nbsp;&nbsp;&nbsp;Settings</a></li>
-<li><a class="reference internal" href="#let-s-start" id="id6">3.2&nbsp;&nbsp;&nbsp;Let's start</a></li>
-<li><a class="reference internal" href="#split-the-template" id="id7">3.3&nbsp;&nbsp;&nbsp;Split the template</a></li>
-<li><a class="reference internal" href="#a-shortcut-for-ajaxed-views" id="id8">3.4&nbsp;&nbsp;&nbsp;A shortcut for ajaxed views</a></li>
-<li><a class="reference internal" href="#pagination" id="id9">3.5&nbsp;&nbsp;&nbsp;Pagination</a></li>
+<li><a class="reference internal" href="#usage" id="id4">3   Usage</a><ul class="auto-toc">
+<li><a class="reference internal" href="#settings" id="id5">3.1   Settings</a></li>
+<li><a class="reference internal" href="#let-s-start" id="id6">3.2   Let's start</a></li>
+<li><a class="reference internal" href="#split-the-template" id="id7">3.3   Split the template</a></li>
+<li><a class="reference internal" href="#a-shortcut-for-ajaxed-views" id="id8">3.4   A shortcut for ajaxed views</a></li>
+<li><a class="reference internal" href="#pagination" id="id9">3.5   Pagination</a></li>
 </ul>
 </li>
-<li><a class="reference internal" href="#templatetags-reference" id="id10">4&nbsp;&nbsp;&nbsp;Templatetags reference</a><ul class="auto-toc">
-<li><a class="reference internal" href="#paginate" id="id11">4.1&nbsp;&nbsp;&nbsp;paginate</a></li>
-<li><a class="reference internal" href="#show-more" id="id12">4.2&nbsp;&nbsp;&nbsp;show_more</a></li>
+<li><a class="reference internal" href="#digg-style-pagination" id="id10">4   Digg-style pagination</a><ul class="auto-toc">
+<li><a class="reference internal" href="#adding-ajax" id="id11">4.1   Adding ajax</a></li>
 </ul>
 </li>
-<li><a class="reference internal" href="#customization" id="id13">5&nbsp;&nbsp;&nbsp;Customization</a><ul class="auto-toc">
-<li><a class="reference internal" href="#template-and-css" id="id14">5.1&nbsp;&nbsp;&nbsp;Template and css</a></li>
+<li><a class="reference internal" href="#templatetags-reference" id="id12">5   Templatetags reference</a><ul class="auto-toc">
+<li><a class="reference internal" href="#paginate" id="id13">5.1   paginate</a></li>
+<li><a class="reference internal" href="#show-more" id="id14">5.2   show_more</a></li>
+<li><a class="reference internal" href="#get-pages" id="id15">5.3   get_pages</a></li>
+<li><a class="reference internal" href="#show-pages" id="id16">5.4   show_pages</a></li>
 </ul>
 </li>
+<li><a class="reference internal" href="#customization" id="id17">6   Customization</a><ul class="auto-toc">
+<li><a class="reference internal" href="#template-and-css" id="id18">6.1   Template and css</a></li>
+</ul>
+</li>
+<li><a class="reference internal" href="#related-projects" id="id19">7   Related projects</a></li>
 </ul>
 </div>
 <div class="section" id="introduction">
-<h1><a class="toc-backref" href="#id1">1&nbsp;&nbsp;&nbsp;Introduction</a></h1>
-<p>This app may be used to provide Twitter-style ajaxed pagination. Future
-developments will add support for normal Digg-style pagination.</p>
+<h1>1   Introduction</h1>
+<p>This app can be used to provide ajaxed Twitter-style or Digg-style pagination.</p>
 <p>The initial idea, which has guided the development of this application,
 is to allow ajax pagination of web contents in very few steps, as done by
 the excellent tool <em>django-pagination</em>
 (see <a class="reference external" href="http://github.com/ericflo/django-pagination/tree/master">http://github.com/ericflo/django-pagination/tree/master</a>).</p>
 </div>
 <div class="section" id="installation">
-<h1><a class="toc-backref" href="#id2">2&nbsp;&nbsp;&nbsp;Installation</a></h1>
+<h1>2   Installation</h1>
 <p>The <tt class="docutils literal"><span class="pre">endless_pagination</span></tt> package, included in the distribution, should be
 placed on the <em>Python path</em>.</p>
 <p>Or just <tt class="docutils literal"><span class="pre">easy_install</span> <span class="pre">django-endless-pagination</span></tt>.</p>
 <div class="section" id="requirements">
-<h2><a class="toc-backref" href="#id3">2.1&nbsp;&nbsp;&nbsp;Requirements</a></h2>
+<h2>2.1   Requirements</h2>
 <ul class="simple">
 <li>Python &gt;= 2.5</li>
 <li>Django &gt;= 1.0</li>
 </div>
 </div>
 <div class="section" id="usage">
-<h1><a class="toc-backref" href="#id4">3&nbsp;&nbsp;&nbsp;Usage</a></h1>
+<h1>3   Usage</h1>
 <div class="section" id="settings">
-<h2><a class="toc-backref" href="#id5">3.1&nbsp;&nbsp;&nbsp;Settings</a></h2>
+<h2>3.1   Settings</h2>
 <p>Add the request context processor in your <em>settings.py</em>, e.g.:</p>
 <pre class="literal-block">
 from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
 )
 </pre>
 <p>Add <tt class="docutils literal"><span class="pre">'endless_pagination'</span></tt> to the <tt class="docutils literal"><span class="pre">INSTALLED_APPS</span></tt> in your <em>settings.py</em>.</p>
-<p>See <em>Customization</em> section of this documentation for other settings options.</p>
+<p>See <em>Customization</em> section in this documentation for other settings options.</p>
 </div>
 <div class="section" id="let-s-start">
-<h2><a class="toc-backref" href="#id6">3.2&nbsp;&nbsp;&nbsp;Let's start</a></h2>
+<h2>3.2   Let's start</h2>
 <p>As creative example, the developer wants pagination of entries of a blog post.</p>
 <p>In <em>views.py</em> we have:</p>
 <pre class="literal-block">
 </pre>
 </div>
 <div class="section" id="split-the-template">
-<h2><a class="toc-backref" href="#id7">3.3&nbsp;&nbsp;&nbsp;Split the template</a></h2>
+<h2>3.3   Split the template</h2>
 <p>A response to an AJAX request should not return the entire template,
 but only the portion of the page to update or add.
 So it is convenient to extrapolate from the template the part containing entries
 </pre>
 </div>
 <div class="section" id="a-shortcut-for-ajaxed-views">
-<h2><a class="toc-backref" href="#id8">3.4&nbsp;&nbsp;&nbsp;A shortcut for ajaxed views</a></h2>
+<h2>3.4   A shortcut for ajaxed views</h2>
 <p>A good practice in writing views is to allow other developers to inject
 the template name and extra data to be added to the context.
 This allows the view to be easily reused. Let's resume the original view
 <p><em>views.py</em>:</p>
 <pre class="literal-block">
 def entry_index(request, template=&quot;myapp/entry_index.html&quot;,
-    extra_context={}):
+    extra_context=None):
     context = {
         'objects': Entry.objects.all(),
     }
-    context.upgrade(extra_context)
+    if extra_context is not None:
+        context.update(extra_context)
     return render_to_response(template, context,
         context_instance=RequestContext(request))
 </pre>
 
 &#64;page_template(&quot;myapp/entry_index_page.html&quot;) # just add this decorator
 def entry_index(request, template=&quot;myapp/entry_index.html&quot;,
-    extra_context={}):
+    extra_context=None):
     context = {
         'objects': Entry.objects.all(),
     }
-    context.upgrade(extra_context)
+    if extra_context is not None:
+        context.update(extra_context)
     return render_to_response(template, context,
         context_instance=RequestContext(request))
 </pre>
 <p>This way, <em>endless-pagination</em> can be included in <strong>generic views</strong> too.</p>
 </div>
 <div class="section" id="pagination">
-<h2><a class="toc-backref" href="#id9">3.5&nbsp;&nbsp;&nbsp;Pagination</a></h2>
+<h2>3.5   Pagination</h2>
 <p>Nothing remains but to change the page template, loading endless templatetags,
 the jQuery library and the javascript file <em>endless.js</em> included
 in the distribution under <tt class="docutils literal"><span class="pre">/media/js/</span></tt>.</p>
 included templatetags.</p>
 </div>
 </div>
+<div class="section" id="digg-style-pagination">
+<h1>4   Digg-style pagination</h1>
+<p>Digg-style pagination of queryset objects is really easy to implement.
+If AJAX pagination is not needed, all you have to do is modify the template, e.g.:</p>
+<pre class="literal-block">
+{% load endless %}
+
+{% paginate objects %}
+{% for object in objects %}
+    {# your code to show the entry #}
+{% endfor %}
+{% show_pages %}
+</pre>
+<p>That's it!
+If you want to display only previous and next links (in a page-by-page pagination)
+you need to use the lower level <em>get_pages</em> templatetag (see reference below),
+e.g.:</p>
+<pre class="literal-block">
+{% load endless %}
+
+{% paginate objects %}
+{% for object in objects %}
+    {# your code to show the entry #}
+{% endfor %}
+{% get_pages %}
+{{ pages.previous }} {{ pages.next }}
+</pre>
+<p>See the paragraph <em>Customization</em> that explains how to customize arrows
+of previous and next pages.</p>
+<div class="section" id="adding-ajax">
+<h2>4.1   Adding ajax</h2>
+<p>The view is exactly the same as in <em>show_more</em> twitter-style pagination:</p>
+<pre class="literal-block">
+from endless_pagination.decorators import page_template
+
+&#64;page_template(&quot;myapp/entry_index_page.html&quot;) # just add this decorator
+def entry_index(request, template=&quot;myapp/entry_index.html&quot;,
+    extra_context=None):
+    context = {
+        'objects': Entry.objects.all(),
+    }
+    if extra_context is not None:
+        context.update(extra_context)
+    return render_to_response(template, context,
+        context_instance=RequestContext(request))
+</pre>
+<p>Of course you have to split templates, but this time a container for
+page template is needed too, and must have a class named <em>endless_page_template</em>.</p>
+<p><em>myapp/entry_index.html</em> becomes:</p>
+<pre class="literal-block">
+{% block js %}
+    {{ block.super }}
+    &lt;script src=&quot;/path/to/jquery.js&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
+    &lt;script src=&quot;/path/to/endless.js&quot; type=&quot;text/javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
+{% endblock %}
+
+&lt;h2&gt;Entries:&lt;/h2&gt;
+&lt;div class=&quot;endless_page_template&quot;&gt;
+    {% include page_template %}
+&lt;/div&gt;
+</pre>
+<p><em>myapp/entry_index_page.html</em> becomes:</p>
+<pre class="literal-block">
+{% load endless %}
+
+{% paginate objects %}
+{% for object in objects %}
+    {# your code to show the entry #}
+{% endfor %}
+{% show_pages %}
+</pre>
+<p>Done.</p>
+</div>
+</div>
 <div class="section" id="templatetags-reference">
-<h1><a class="toc-backref" href="#id10">4&nbsp;&nbsp;&nbsp;Templatetags reference</a></h1>
+<h1>5   Templatetags reference</h1>
 <div class="section" id="paginate">
-<h2><a class="toc-backref" href="#id11">4.1&nbsp;&nbsp;&nbsp;paginate</a></h2>
+<h2>5.1   paginate</h2>
 <p>Usage:</p>
 <pre class="literal-block">
 {% paginate objects %}
 <pre class="literal-block">
 {% paginate 20 objects as paginated_objects %}
 </pre>
-<p>You must use this tag before calling the <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">show_more</span> <span class="pre">%}</span></tt> one.</p>
+<p>You must use this tag before calling <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">show_more</span> <span class="pre">%}</span></tt> or <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">show_pages</span> <span class="pre">%}</span></tt>.</p>
 </div>
 <div class="section" id="show-more">
-<h2><a class="toc-backref" href="#id12">4.2&nbsp;&nbsp;&nbsp;show_more</a></h2>
+<h2>5.2   show_more</h2>
 <p>Show the link to get the next page in a Twitter-like pagination.
 Usage:</p>
 <pre class="literal-block">
 </pre>
 <p>Must be called after <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">paginate</span> <span class="pre">objects</span> <span class="pre">%}</span></tt>.</p>
 </div>
+<div class="section" id="get-pages">
+<h2>5.3   get_pages</h2>
+<p>Usage:</p>
+<pre class="literal-block">
+{% get_pages %}
+</pre>
+<p>This is mostly used for digg-style pagination.
+This call inserts in the template context a <em>pages</em> variable, as a sequence
+of page links. You can use <em>pages</em> in different ways:</p>
+<p>just print <em>pages</em> and you will get digg-style pagination displayed:</p>
+<pre class="literal-block">
+{{ pages }}
+</pre>
+<p>display pages count:</p>
+<pre class="literal-block">
+{{ pages|length }}
+</pre>
+<p>get a specific page:</p>
+<pre class="literal-block">
+{# the current selected page #}
+{{ pages.current }}
+
+{# the first page #}
+{{ pages.first }}
+
+{# the last page #}
+{{ pages.last }}
+
+{# the previous page (or nothing if you are on first page) #}
+{{ pages.previous }}
+
+{# the next page (or nothing if you are in last page) #}
+{{ pages.next }}
+
+{# the third page #}
+{{ pages.3 }}
+{# this means page.1 is the same as page.first #}
+</pre>
+<p>iterate over <em>pages</em> to get all pages:</p>
+<pre class="literal-block">
+{% for page in pages %}
+    {# display page link #}
+    {{ page }}
+
+    {# the page url (beginning with &quot;?&quot;) #}
+    {{ page.url }}
+
+    {# the page path #}
+    {{ page.path }}
+
+    {# the page number #}
+    {{ page.number }}
+
+    {# a string representing the page (commonly the page number) #}
+    {{ page.label }}
+
+    {# check if the page is the current one #}
+    {{ page.is_current }}
+
+    {# check if the page is the first one #}
+    {{ page.is_first }}
+
+    {# check if the page is the last one #}
+    {{ page.is_last }}
+{% endfor %}
+</pre>
+<p>You can change the variable name, e.g.:</p>
+<pre class="literal-block">
+{% get_pages as page_links %}
+</pre>
+<p>Must be called after <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">paginate</span> <span class="pre">objects</span> <span class="pre">%}</span></tt>.</p>
+</div>
+<div class="section" id="show-pages">
+<h2>5.4   show_pages</h2>
+<p>Show page links.
+Usage:</p>
+<pre class="literal-block">
+{% show_pages %}
+</pre>
+<p>It is only a shortcut for:</p>
+<pre class="literal-block">
+{% get_pages %}
+{{ pages }}
+</pre>
+<p>You can set <em>ENDLESS_PAGE_LIST_CALLABLE</em> in your settings.py as a callable
+used to customize the pages that are displayed.
+The callable takes the current page number and the total number of pages
+and must return a sequence of page numbers that will be displayed.
+The sequence can contain other values:</p>
+<blockquote>
+<ul class="simple">
+<li><em>&quot;previous&quot;</em>: will display the previous page in that position</li>
+<li><em>&quot;next&quot;</em>: will display the next page in that position</li>
+<li><em>None</em>: a separator will be displayed in that position</li>
+</ul>
+</blockquote>
+<p>Here is an example of custom callable that displays previous page, then
+first page, then a separator, then current page, then next page:</p>
+<pre class="literal-block">
+def get_page_numbers(current_page, num_pages):
+    return (&quot;previous&quot;, 1, &quot;...&quot;, current_page, &quot;next&quot;)
+</pre>
+<p>If <em>ENDLESS_PAGE_LIST_CALLABLE</em> is <em>None</em> an internal callable is used,
+generating a digg-style pagination.</p>
+<p>Must be called after <tt class="docutils literal"><span class="pre">{%</span> <span class="pre">paginate</span> <span class="pre">objects</span> <span class="pre">%}</span></tt>.</p>
+</div>
 </div>
 <div class="section" id="customization">
-<h1><a class="toc-backref" href="#id13">5&nbsp;&nbsp;&nbsp;Customization</a></h1>
+<h1>6   Customization</h1>
 <p>You can customize the application using <tt class="docutils literal"><span class="pre">settings.py</span></tt>.</p>
-<ul>
-<li><p class="first"><em>ENDLESS_PAGINATION_PER_PAGE</em> (default=10):
-How many objects are normally displayed in a page (overwriteable by templatetag).</p>
-</li>
-<li><p class="first"><em>ENDLESS_PAGINATION_PAGE_LABEL</em> (default=&quot;page&quot;):
-The querystring key of the page number (e.g. <tt class="docutils literal"><span class="pre">http://example.com?page=2</span></tt>)</p>
-</li>
-<li><p class="first"><em>ENDLESS_PAGINATION_ORPHANS</em> (default=0):
-See django <em>Paginator</em> definition of orphans.</p>
-</li>
-<li><p class="first"><em>ENDLESS_PAGINATION_LOADING</em> (default=&quot;loading&quot;):
+<ul class="simple">
+<li><em>ENDLESS_PAGINATION_PER_PAGE</em> (default=10):
+How many objects are normally displayed in a page (overwriteable by templatetag).</li>
+<li><em>ENDLESS_PAGINATION_PAGE_LABEL</em> (default=&quot;page&quot;):
+The querystring key of the page number (e.g. <tt class="docutils literal"><span class="pre">http://example.com?page=2</span></tt>)</li>
+<li><em>ENDLESS_PAGINATION_ORPHANS</em> (default=0):
+See django <em>Paginator</em> definition of orphans.</li>
+<li><em>ENDLESS_PAGINATION_LOADING</em> (default=&quot;loading&quot;):
 If you use the default <em>show_more</em> template, here you can customize
 the content of the loader hidden element
-Html is safe here, e.g. you can show your pretty animated gif:</p>
+Html is safe here, e.g. you can show your pretty animated gif</li>
+</ul>
 <pre class="literal-block">
 ENDLESS_PAGINATION_LOADING = &quot;&quot;&quot;
     &lt;img src=&quot;/site_media/img/loader.gif&quot; alt=&quot;loading&quot; /&gt;
 &quot;&quot;&quot;
 </pre>
+<ul>
+<li><p class="first"><em>ENDLESS_PAGINATION_PREVIOUS_LABEL</em> (default=u&quot;&amp;lt;&amp;lt;&quot;) and <em>NEXT_LABEL</em> (default=u&quot;&amp;gt;&amp;gt;&quot;):
+Labels for previous and next page links.</p>
+</li>
+<li><p class="first"><em>ENDLESS_PAGINATION_PAGE_LIST_CALLABLE</em> (default=None):
+Callable that returns pages to be displayed.
+If None a default callable is used (that produces digg-style pagination).</p>
+<p>Default callable returns pages for digg-style pagination, and depends
+on the settings below:</p>
+</li>
+<li><p class="first"><em>ENDLESS_PAGINATION_DEFAULT_CALLABLE_EXTREMES</em> (default=3)</p>
+</li>
+<li><p class="first"><em>ENDLESS_PAGINATION_DEFAULT_CALLABLE_AROUNDS</em> (default=2)</p>
 </li>
 </ul>
 <div class="section" id="template-and-css">
-<h2><a class="toc-backref" href="#id14">5.1&nbsp;&nbsp;&nbsp;Template and css</a></h2>
+<h2>6.1   Template and css</h2>
 <p>You can override the default template for <em>show_more</em> templatetag following
 some rules:</p>
 <ul class="simple">
 <p>Application comes with English and Italian i18n.</p>
 </div>
 </div>
+<div class="section" id="related-projects">
+<h1>7   Related projects</h1>
+<p>Try out <a class="reference external" href="http://code.google.com/p/django-yafinder/">http://code.google.com/p/django-yafinder/</a> if you need to add filter
+and sort capabilities to your index page.</p>
+</div>
 </div>
 </body>
 </html>
 
 .. sectnum::
 
+
 Introduction
 ============
 
-This app may be used to provide Twitter-style ajaxed pagination. Future
-developments will add support for normal Digg-style pagination.
+This app can be used to provide ajaxed Twitter-style or Digg-style pagination.
 
 The initial idea, which has guided the development of this application, 
 is to allow ajax pagination of web contents in very few steps, as done by 
     
 Add ``'endless_pagination'`` to the ``INSTALLED_APPS`` in your *settings.py*.
 
-See *Customization* section of this documentation for other settings options.
+See *Customization* section in this documentation for other settings options.
 
 Let's start
 ~~~~~~~~~~~
 *views.py*::
 
     def entry_index(request, template="myapp/entry_index.html", 
-        extra_context={}):
+        extra_context=None):
         context = {
             'objects': Entry.objects.all(),
         }
-        context.upgrade(extra_context)
+        if extra_context is not None:
+            context.update(extra_context)
         return render_to_response(template, context, 
             context_instance=RequestContext(request))
 
     
     @page_template("myapp/entry_index_page.html") # just add this decorator
     def entry_index(request, template="myapp/entry_index.html", 
-        extra_context={}):
+        extra_context=None):
         context = {
             'objects': Entry.objects.all(),
         }
-        context.upgrade(extra_context)
+        if extra_context is not None:
+            context.update(extra_context)
         return render_to_response(template, context, 
             context_instance=RequestContext(request))
 
 included templatetags.
 
 
+Digg-style pagination
+=====================
+
+Digg-style pagination of queryset objects is really easy to implement.
+If AJAX pagination is not needed, all you have to do is modify the template, e.g.::
+
+    {% load endless %}
+    
+    {% paginate objects %}
+    {% for object in objects %}
+        {# your code to show the entry #}
+    {% endfor %}
+    {% show_pages %}
+    
+That's it!
+If you want to display only previous and next links (in a page-by-page pagination)
+you need to use the lower level *get_pages* templatetag (see reference below),
+e.g.::
+
+    {% load endless %}
+    
+    {% paginate objects %}
+    {% for object in objects %}
+        {# your code to show the entry #}
+    {% endfor %}
+    {% get_pages %}
+    {{ pages.previous }} {{ pages.next }}
+
+See the paragraph *Customization* that explains how to customize arrows
+of previous and next pages.
+
+Adding ajax
+~~~~~~~~~~~
+
+The view is exactly the same as in *show_more* twitter-style pagination::
+
+    from endless_pagination.decorators import page_template
+    
+    @page_template("myapp/entry_index_page.html") # just add this decorator
+    def entry_index(request, template="myapp/entry_index.html", 
+        extra_context=None):
+        context = {
+            'objects': Entry.objects.all(),
+        }
+        if extra_context is not None:
+            context.update(extra_context)
+        return render_to_response(template, context, 
+            context_instance=RequestContext(request))
+            
+Of course you have to split templates, but this time a container for 
+page template is needed too, and must have a class named *endless_page_template*.
+
+*myapp/entry_index.html* becomes::
+
+    {% block js %}
+        {{ block.super }}
+        <script src="/path/to/jquery.js" type="text/javascript" charset="utf-8"></script>
+        <script src="/path/to/endless.js" type="text/javascript" charset="utf-8"></script>
+    {% endblock %}
+    
+    <h2>Entries:</h2>
+    <div class="endless_page_template">
+        {% include page_template %}
+    </div>
+
+*myapp/entry_index_page.html* becomes::
+
+    {% load endless %}
+    
+    {% paginate objects %}
+    {% for object in objects %}
+        {# your code to show the entry #}
+    {% endfor %}
+    {% show_pages %}
+    
+Done.
+    
+
 Templatetags reference
 ======================
 
 
     {% paginate 20 objects as paginated_objects %}
     
-You must use this tag before calling the ``{% show_more %}`` one.
+You must use this tag before calling ``{% show_more %}`` or ``{% show_pages %}``.
 
 show_more
 ~~~~~~~~~
     
 Must be called after ``{% paginate objects %}``.
 
+get_pages
+~~~~~~~~~
+
+Usage::
+
+    {% get_pages %}
+
+This is mostly used for digg-style pagination.
+This call inserts in the template context a *pages* variable, as a sequence
+of page links. You can use *pages* in different ways:
+
+just print *pages* and you will get digg-style pagination displayed::
+
+    {{ pages }}
+    
+display pages count::
+
+    {{ pages|length }}
+    
+get a specific page::
+    
+    {# the current selected page #}
+    {{ pages.current }} 
+    
+    {# the first page #}
+    {{ pages.first }} 
+    
+    {# the last page #}
+    {{ pages.last }} 
+    
+    {# the previous page (or nothing if you are on first page) #}
+    {{ pages.previous }} 
+    
+    {# the next page (or nothing if you are in last page) #}
+    {{ pages.next }}
+    
+    {# the third page #}
+    {{ pages.3 }}
+    {# this means page.1 is the same as page.first #}
+    
+iterate over *pages* to get all pages::
+
+    {% for page in pages %}
+        {# display page link #}
+        {{ page }} 
+        
+        {# the page url (beginning with "?") #}
+        {{ page.url }} 
+        
+        {# the page path #}
+        {{ page.path }} 
+        
+        {# the page number #}
+        {{ page.number }} 
+        
+        {# a string representing the page (commonly the page number) #}
+        {{ page.label }}
+        
+        {# check if the page is the current one #}
+        {{ page.is_current }}
+        
+        {# check if the page is the first one #}
+        {{ page.is_first }}
+        
+        {# check if the page is the last one #}
+        {{ page.is_last }} 
+    {% endfor %}
+    
+You can change the variable name, e.g.::
+
+    {% get_pages as page_links %}
+
+Must be called after ``{% paginate objects %}``.
+
+show_pages
+~~~~~~~~~~
+
+Show page links.
+Usage::
+
+    {% show_pages %}
+    
+It is only a shortcut for::
+
+    {% get_pages %}
+    {{ pages }}
+
+You can set *ENDLESS_PAGE_LIST_CALLABLE* in your settings.py as a callable 
+used to customize the pages that are displayed.
+The callable takes the current page number and the total number of pages
+and must return a sequence of page numbers that will be displayed.
+The sequence can contain other values:
+
+    - *"previous"*: will display the previous page in that position
+    - *"next"*: will display the next page in that position
+    - *None*: a separator will be displayed in that position
+    
+Here is an example of custom callable that displays previous page, then
+first page, then a separator, then current page, then next page::
+
+    def get_page_numbers(current_page, num_pages):
+        return ("previous", 1, "...", current_page, "next")
+
+If *ENDLESS_PAGE_LIST_CALLABLE* is *None* an internal callable is used,
+generating a digg-style pagination.
+
+Must be called after ``{% paginate objects %}``.
+
 
 Customization
 =============
 - *ENDLESS_PAGINATION_LOADING* (default="loading"):
   If you use the default *show_more* template, here you can customize
   the content of the loader hidden element
-  Html is safe here, e.g. you can show your pretty animated gif::
+  Html is safe here, e.g. you can show your pretty animated gif
   
+
+
+::
+
      ENDLESS_PAGINATION_LOADING = """
          <img src="/site_media/img/loader.gif" alt="loading" />
      """
+  
+     
+- *ENDLESS_PAGINATION_PREVIOUS_LABEL* (default=u"&lt;&lt;") and *NEXT_LABEL* (default=u"&gt;&gt;"):
+  Labels for previous and next page links.
+  
+- *ENDLESS_PAGINATION_PAGE_LIST_CALLABLE* (default=None):
+  Callable that returns pages to be displayed.
+  If None a default callable is used (that produces digg-style pagination).
+  
+  Default callable returns pages for digg-style pagination, and depends
+  on the settings below:
+  
+- *ENDLESS_PAGINATION_DEFAULT_CALLABLE_EXTREMES* (default=3)
+- *ENDLESS_PAGINATION_DEFAULT_CALLABLE_AROUNDS* (default=2)
      
 Template and css
 ~~~~~~~~~~~~~~~~
 - the loader hidden element class is *endless_loading*
 
 Application comes with English and Italian i18n.
+
+
+Related projects
+================
+
+Try out http://code.google.com/p/django-yafinder/ if you need to add filter
+and sort capabilities to your index page.
+
-== Endless Pagination ==
+= Endless Pagination =
 
-Author: Francesco Banconi <[mailto:francesco.banconi@gmail.com francesco.banconi@gmail.com]>
+|| Author: || Francesco Banconi <[mailto:francesco.banconi@gmail.com francesco.banconi@gmail.com]> ||
 
 
-== 1   Introduction ==
+= 1 Introduction =
 
-This app may be used to provide Twitter-style ajaxed pagination. Future developments will add support for normal Digg-style pagination.
+This app can be used to provide ajaxed Twitter-style or Digg-style pagination.
 
-The initial idea, which has guided the development of this application, is to allow ajax pagination of web contents in very few steps, as done by the excellent tool *django-pagination*(see [http://github.com/ericflo/django-pagination/tree/master http://github.com/ericflo/django-pagination/tree/master]).
+The initial idea, which has guided the development of this application, is to allow ajax pagination of web contents in very few steps, as done by the excellent tool _django-pagination_ (see http://github.com/ericflo/django-pagination/tree/master).
 
-== 2   Installation ==
+= 2 Installation =
 
-The `endless_pagination` package, included in the distribution, should be placed on the *Python path*.
+The `endless_pagination` package, included in the distribution, should be placed on the _Python path_.
 
 Or just `easy_install django-endless-pagination`.
 
-== 2.1   Requirements ==
+== 2.1 Requirements ==
 
- * Python >= 2.5
+  * Python >= 2.5
+  * Django >= 1.0
+  * jQuery >= 1.3
 
- * Django >= 1.0
+= 3 Usage =
 
- * jQuery >= 1.3
+== 3.1 Settings ==
 
-
-
-== 3   Usage ==
-
-== 3.1   Settings ==
-
-Add the request context processor in your *settings.py*, e.g.:
+Add the request context processor in your _settings.py_, e.g.:
 
 {{{
+
 from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
 TEMPLATE_CONTEXT_PROCESSORS += (
      'django.core.context_processors.request',
 )
+
 }}}
 
-Add `'endless_pagination'` to the `INSTALLED_APPS` in your *settings.py*.
+Add `'endless_pagination'` to the `INSTALLED_APPS` in your _settings.py_.
 
-See *Customization* section of this documentation for other settings options.
+See _Customization_ section in this documentation for other settings options.
 
-== 3.2   Let's start ==
+== 3.2 Let's start ==
 
 As creative example, the developer wants pagination of entries of a blog post.
 
-In *views.py* we have:
+In _views.py_ we have:
 
 {{{
+
 def entry_index(request, template="myapp/entry_index.html"):
     context = {
         'objects': Entry.objects.all(),
     }
     return render_to_response(template, context,
         context_instance=RequestContext(request))
+
 }}}
 
-In *myapp/entry_index.html*:
+In _myapp/entry_index.html_:
 
 {{{
+
 <h2>Entries:</h2>
 {% for object in objects %}
     {# your code to show the entry #}
 {% endfor %}
+
 }}}
 
-== 3.3   Split the template ==
+== 3.3 Split the template ==
 
 A response to an AJAX request should not return the entire template, but only the portion of the page to update or add. So it is convenient to extrapolate from the template the part containing entries and use the new one to render the context if the request is AJAX. The main template will include the other, so it is convenient to put the page template in the context.
 
-*views.py* becomes:
+_views.py_ becomes:
 
 {{{
+
 def entry_index(request,
     template="myapp/entry_index.html",
     page_template="myapp/entry_index_page.html"):
         template = page_template
     return render_to_response(template, context,
         context_instance=RequestContext(request))
+
 }}}
 
-See below how to obtain the same result just decorating the view(in a way compatible with generic views too).
+See below how to obtain the same result *just decorating the view* (in a way compatible with generic views too).
 
-*myapp/entry_index.html* becomes:
+_myapp/entry_index.html_ becomes:
 
 {{{
+
 <h2>Entries:</h2>
 {% include page_template %}
+
 }}}
 
-*myapp/entry_index_page.html* becomes:
+_myapp/entry_index_page.html_ becomes:
 
 {{{
+
 {% for object in objects %}
     {# your code to show the entry #}
 {% endfor %}
+
 }}}
 
-== 3.4   A shortcut for ajaxed views ==
+== 3.4 A shortcut for ajaxed views ==
 
 A good practice in writing views is to allow other developers to inject the template name and extra data to be added to the context. This allows the view to be easily reused. Let's resume the original view with extra context injection:
 
-*views.py*:
+_views.py_:
 
 {{{
+
 def entry_index(request, template="myapp/entry_index.html",
-    extra_context={}):
+    extra_context=None):
     context = {
         'objects': Entry.objects.all(),
     }
-    context.upgrade(extra_context)
+    if extra_context is not None:
+        context.update(extra_context)
     return render_to_response(template, context,
         context_instance=RequestContext(request))
+
 }}}
 
 Splitting templates and putting the ajax template name in the context is easily achievable at this point (using a builtin decorator).
 
-*views.py* becomes:
+_views.py_ becomes:
 
 {{{
+
 from endless_pagination.decorators import page_template
 
 @page_template("myapp/entry_index_page.html") # just add this decorator
 def entry_index(request, template="myapp/entry_index.html",
-    extra_context={}):
+    extra_context=None):
     context = {
         'objects': Entry.objects.all(),
     }
-    context.upgrade(extra_context)
+    if extra_context is not None:
+        context.update(extra_context)
     return render_to_response(template, context,
         context_instance=RequestContext(request))
+
 }}}
 
-This way, *endless-pagination* can be included in generic views too.
+This way, _endless-pagination_ can be included in *generic views* too.
 
-== 3.5   Pagination ==
+== 3.5 Pagination ==
 
-Nothing remains but to change the page template, loading endless templatetags,
-the jQuery library and the javascript file *endless.js* included 
-in the distribution under {{{/media/js/}}}.
+Nothing remains but to change the page template, loading endless templatetags, the jQuery library and the javascript file _endless.js_ included in the distribution under `/media/js/`.
 
-*myapp/entry_index.html* becomes:
+_myapp/entry_index.html_ becomes:
 
 {{{
+
 {% block js %}
     {{ block.super }}
     <script src="/path/to/jquery.js" type="text/javascript" charset="utf-8"></script>
 
 <h2>Entries:</h2>
 {% include page_template %}
+
 }}}
 
-*myapp/entry_index_page.html* becomes:
+_myapp/entry_index_page.html_ becomes:
 
 {{{
+
 {% load endless %}
 
 {% paginate objects %}
     {# your code to show the entry #}
 {% endfor %}
 {% show_more %}
+
 }}}
 
 That's all. Read the next section of the documentation to improve the use of included templatetags.
 
-== 4   Templatetags reference ==
+= 4 Digg-style pagination =
 
-== 4.1   paginate ==
+Digg-style pagination of queryset objects is really easy to implement. If AJAX pagination is not needed, all you have to do is modify the template, e.g.:
+
+{{{
+
+{% load endless %}
+
+{% paginate objects %}
+{% for object in objects %}
+    {# your code to show the entry #}
+{% endfor %}
+{% show_pages %}
+
+}}}
+
+That's it! If you want to display only previous and next links (in a page-by-page pagination) you need to use the lower level _get_pages_ templatetag (see reference below), e.g.:
+
+{{{
+
+{% load endless %}
+
+{% paginate objects %}
+{% for object in objects %}
+    {# your code to show the entry #}
+{% endfor %}
+{% get_pages %}
+{{ pages.previous }} {{ pages.next }}
+
+}}}
+
+See the paragraph _Customization_ that explains how to customize arrows of previous and next pages.
+
+== 4.1 Adding ajax ==
+
+The view is exactly the same as in _show_more_ twitter-style pagination:
+
+{{{
+
+from endless_pagination.decorators import page_template
+
+@page_template("myapp/entry_index_page.html") # just add this decorator
+def entry_index(request, template="myapp/entry_index.html",
+    extra_context=None):
+    context = {
+        'objects': Entry.objects.all(),
+    }
+    if extra_context is not None:
+        context.update(extra_context)
+    return render_to_response(template, context,
+        context_instance=RequestContext(request))
+
+}}}
+
+Of course you have to split templates, but this time a container for page template is needed too, and must have a class named _endless_page_template_.
+
+_myapp/entry_index.html_ becomes:
+
+{{{
+
+{% block js %}
+    {{ block.super }}
+    <script src="/path/to/jquery.js" type="text/javascript" charset="utf-8"></script>
+    <script src="/path/to/endless.js" type="text/javascript" charset="utf-8"></script>
+{% endblock %}
+
+<h2>Entries:</h2>
+<div class="endless_page_template">
+    {% include page_template %}
+</div>
+
+}}}
+
+_myapp/entry_index_page.html_ becomes:
+
+{{{
+
+{% load endless %}
+
+{% paginate objects %}
+{% for object in objects %}
+    {# your code to show the entry #}
+{% endfor %}
+{% show_pages %}
+
+}}}
+
+Done.
+
+= 5 Templatetags reference =
+
+== 5.1 paginate ==
 
 Usage:
 
 {{{
+
 {% paginate objects %}
+
 }}}
 
-After this call, in the template context the *objects* variable is replaced with only the objects of the current page.
+After this call, in the template context the _objects_ variable is replaced with only the objects of the current page.
 
-You can also mantain your *objects* original variable (commonly a queryset) and add to context another name referring to objects of the current page, e.g.:
+You can also mantain your _objects_ original variable (commonly a queryset) and add to context another name referring to objects of the current page, e.g.:
 
 {{{
+
 {% paginate objects as page_objects %}
+
 }}}
 
 The number of paginated object is taken from settings, but you can override the default, e.g.:
 
 {{{
+
 {% paginate 20 objects %}
+
 }}}
 
 Of course you can mix it all:
 
 {{{
+
 {% paginate 20 objects as paginated_objects %}
+
 }}}
 
-You must use this tag before calling the `{% show_more %}` one.
+You must use this tag before calling `{% show_more %}` or `{% show_pages %}`.
 
-== 4.2   show_more ==
+== 5.2 show_more ==
 
 Show the link to get the next page in a Twitter-like pagination. Usage:
 
 {{{
+
 {% show_more %}
+
 }}}
 
 Must be called after `{% paginate objects %}`.
 
-== 5   Customization ==
+== 5.3 get_pages ==
+
+Usage:
+
+{{{
+
+{% get_pages %}
+
+}}}
+
+This is mostly used for digg-style pagination. This call inserts in the template context a _pages_ variable, as a sequence of page links. You can use _pages_ in different ways:
+
+just print _pages_ and you will get digg-style pagination displayed:
+
+{{{
+
+{{ pages }}
+
+}}}
+
+display pages count:
+
+{{{
+
+{{ pages|length }}
+
+}}}
+
+get a specific page:
+
+{{{
+
+{# the current selected page #}
+{{ pages.current }}
+
+{# the first page #}
+{{ pages.first }}
+
+{# the last page #}
+{{ pages.last }}
+
+{# the previous page (or nothing if you are on first page) #}
+{{ pages.previous }}
+
+{# the next page (or nothing if you are in last page) #}
+{{ pages.next }}
+
+{# the third page #}
+{{ pages.3 }}
+{# this means page.1 is the same as page.first #}
+
+}}}
+
+iterate over _pages_ to get all pages:
+
+{{{
+
+{% for page in pages %}
+    {# display page link #}
+    {{ page }}
+
+    {# the page url (beginning with "?") #}
+    {{ page.url }}
+
+    {# the page path #}
+    {{ page.path }}
+
+    {# the page number #}
+    {{ page.number }}
+
+    {# a string representing the page (commonly the page number) #}
+    {{ page.label }}
+
+    {# check if the page is the current one #}
+    {{ page.is_current }}
+
+    {# check if the page is the first one #}
+    {{ page.is_first }}
+
+    {# check if the page is the last one #}
+    {{ page.is_last }}
+{% endfor %}
+
+}}}
+
+You can change the variable name, e.g.:
+
+{{{
+
+{% get_pages as page_links %}
+
+}}}
+
+Must be called after `{% paginate objects %}`.
+
+== 5.4 show_pages ==
+
+Show page links. Usage:
+
+{{{
+
+{% show_pages %}
+
+}}}
+
+It is only a shortcut for:
+
+{{{
+
+{% get_pages %}
+{{ pages }}
+
+}}}
+
+You can set _ENDLESS_PAGE_LIST_CALLABLE_ in your settings.py as a callable used to customize the pages that are displayed. The callable takes the current page number and the total number of pages and must return a sequence of page numbers that will be displayed. The sequence can contain other values:
+
+  * _"previous"_: will display the previous page in that position
+  * _"next"_: will display the next page in that position
+  * _None_: a separator will be displayed in that position
+
+Here is an example of custom callable that displays previous page, then first page, then a separator, then current page, then next page:
+
+{{{
+
+def get_page_numbers(current_page, num_pages):
+    return ("previous", 1, "...", current_page, "next")
+
+}}}
+
+If _ENDLESS_PAGE_LIST_CALLABLE_ is _None_ an internal callable is used, generating a digg-style pagination.
+
+Must be called after `{% paginate objects %}`.
+
+= 6 Customization =
 
 You can customize the application using `settings.py`.
 
- * *ENDLESS_PAGINATION_PER_PAGE* (default=10): How many objects are normally displayed in a page (overwriteable by templatetag).
-
- * *ENDLESS_PAGINATION_PAGE_LABEL* (default="page"): The querystring key of the page number (e.g. `http://example.com?page=2`)
-
- * *ENDLESS_PAGINATION_ORPHANS* (default=0): See django *Paginator* definition of orphans.
-
- * *ENDLESS_PAGINATION_LOADING* (default="loading"): If you use the default *show_more* template, here you can customize the content of the loader hidden element Html is safe here, e.g. you can show your pretty animated gif:
+  * _ENDLESS_PAGINATION_PER_PAGE_ (default=10): How many objects are normally displayed in a page (overwriteable by templatetag).
+  * _ENDLESS_PAGINATION_PAGE_LABEL_ (default="page"): The querystring key of the page number (e.g. `http://example.com?page=2`)
+  * _ENDLESS_PAGINATION_ORPHANS_ (default=0): See django _Paginator_ definition of orphans.
+  * _ENDLESS_PAGINATION_LOADING_ (default="loading"): If you use the default _show_more_ template, here you can customize the content of the loader hidden element Html is safe here, e.g. you can show your pretty animated gif
 
 {{{
+
 ENDLESS_PAGINATION_LOADING = """
     <img src="/site_media/img/loader.gif" alt="loading" />
 """
+
 }}}
 
-== 5.1   Template and css ==
+  * _ENDLESS_PAGINATION_PREVIOUS_LABEL_ (default=u"&lt;&lt;") and _NEXT_LABEL_ (default=u"&gt;&gt;"): Labels for previous and next page links.
+  * _ENDLESS_PAGINATION_PAGE_LIST_CALLABLE_ (default=None): Callable that returns pages to be displayed. If None a default callable is used (that produces digg-style pagination).Default callable returns pages for digg-style pagination, and depends on the settings below:
+  * _ENDLESS_PAGINATION_DEFAULT_CALLABLE_EXTREMES_ (default=3)
+  * _ENDLESS_PAGINATION_DEFAULT_CALLABLE_AROUNDS_ (default=2)
 
-You can override the default template for *show_more* templatetag following
-some rules:
+== 6.1 Template and css ==
 
- * *more* link is showed only if variable ``querystring`` is not False
- * the container (most external html element) class is *endless_container*
- * the *more* link and the loader hidden element live inside the container
- * the *more* link class is *endless_more*
- * the loader hidden element class is *endless_loading*
+You can override the default template for _show_more_ templatetag following some rules:
+
+  * _more_ link is showed only if variable `querystring` is not False
+  * the container (most external html element) class is _endless_container_
+  * the _more_ link and the loader hidden element live inside the container
+  * the _more_ link class is _endless_more_
+  * the loader hidden element class is _endless_loading_
 
 Application comes with English and Italian i18n.
 
+= 7 Related projects =
+
+Try out http://code.google.com/p/django-yafinder/ if you need to add filter and sort capabilities to your index page.

endless_pagination/__init__.py

-VERSION = (0, 2)
+VERSION = (0, 3)

endless_pagination/models.py

+from django.template import Context, loader
+
+from endless_pagination import settings, utils
+
+# preload page templates
+PAGE_TEMPLATE = loader.get_template("endless/page_link.html")
+CURRENT_TEMPLATE = loader.get_template("endless/current_link.html")
+
+class EndlessPage(object):
+    """
+    A page link representation.
+    Interesting attributes:
+    
+        - *self.number*: the page number
+        - *self.label*: the label of the link (normally the page number as string)
+        - *self.url*: the url of the page (strting with "?")
+        - *self.path*: the path of the page
+        - *self.is_current*: return True if page is the current page displayed
+        - *self.is_first*: return True if page is the first page
+        - *self.is_last*:  return True if page is the last page
+    """
+    def __init__(self, request, number, current_number, total_number, label=None):
+        self.number = number
+        self.label = unicode(number) if label is None else label
+        
+        self.is_current = number == current_number
+        self.is_first = number == 1
+        self.is_last = number == total_number
+        
+        self.url = utils.get_querystring_for_page(request, number)
+        self.path = "%s%s" % (request.path, self.url)
+
+    def __unicode__(self):
+        """
+        Render the page as a link.
+        """
+        context_instance = Context({'page': self})
+        template = CURRENT_TEMPLATE if self.is_current else PAGE_TEMPLATE
+        return template.render(context_instance)
+                
+        
+class PageList(object):
+    """
+    A sequence of endless pages.
+    """
+    def __init__(self, request, page):
+        self._request = request
+        self._page = page
+    
+    def __getitem__(self, value):
+        # type conversion is needed beacuse in templates django performs a 
+        # dictionary lookup before the attribute lokups 
+        # (when a dot is encountered)
+        try:
+            value = int(value)
+        except (TypeError, ValueError):
+            # a TypeError says to django to continue with an attribute lookup
+            raise TypeError
+        if 1 <= value <= len(self): 
+            return EndlessPage(self._request, value, self._page.number, len(self))
+        raise IndexError("page list index out of range")
+        
+    def __len__(self):
+        """
+        The length of the sequence is the total number of pages.
+        """
+        return self._page.paginator.num_pages
+    
+    def __iter__(self):
+        """
+        Iterate over all the endless pages (from first to last).
+        """
+        for i in range(len(self)):
+            yield self[i+1]
+        
+    def __unicode__(self):
+        """
+        Return digg-style pagination (by default).
+        The callable *settings.PAGE_LIST_CALLABLE* can be used to customize
+        the pages that are displayed.
+        The callable takes the current page number and the total number of pages
+        and must return a sequence of page numbers that will be displayed.
+        The sequence can contain other values:
+        
+            - *"previous"*: will display the previous page in that position
+            - *"next"*: will display the next page in that position
+            - *None*: a separator will be displayed in that position
+            
+        Here is an example of custom calable that displays previous page, then
+        first page, then a separator, then current page, then next page::
+        
+            def get_page_numbers(current_page, num_pages):
+                return ("previous", 1, None, current_page, "next")
+        
+        If *settings.PAGE_LIST_CALLABLE* is None an internal callable is used,
+        generating a digg-style pagination.
+        """
+        if len(self) > 1:
+            pages_callable = settings.PAGE_LIST_CALLABLE or utils.get_page_numbers
+            pages = []
+            for i in pages_callable(self._page.number, len(self)):
+                if i is None:
+                    pages.append(i)
+                elif i == "previous":
+                    pages.append(self.previous())
+                elif i == "next":
+                    pages.append(self.next())
+                else:
+                    pages.append(self[i])
+            context = {'pages': pages}
+            return loader.render_to_string("endless/show_pages.html", context)
+        return u""
+        
+    def current(self):
+        """
+        Return current page.
+        """
+        return self[self._page.number]
+        
+    def first(self):
+        """
+        Return first page.
+        """
+        return self[1]
+        
+    def last(self):
+        """
+        Return last page.
+        """
+        return self[len(self)]
+        
+    def previous(self):
+        """
+        Return previous page or an empty string if current page is the first.
+        """
+        if self._page.has_previous():
+            return EndlessPage(self._request, self._page.previous_page_number(), 
+                self._page.number, len(self), label=settings.PREVIOUS_LABEL)
+        return u""
+        
+    def next(self):
+        """
+        Return next page or an empty string if current page is the last.
+        """
+        if self._page.has_next():
+            return EndlessPage(self._request, self._page.next_page_number(), 
+                self._page.number, len(self), label=settings.NEXT_LABEL)
+        return u""

endless_pagination/settings.py

 #    """
 LOADING = getattr(settings, 
     "ENDLESS_PAGINATION_LOADING", "loading")
+
+# Labels for previous and next page links.
+PREVIOUS_LABEL = getattr(settings, "ENDLESS_PAGINATION_PREVIOUS_LABEL", u"&lt;&lt;")
+NEXT_LABEL = getattr(settings, "ENDLESS_PAGINATION_NEXT_LABEL", u"&gt;&gt;")
+
+# Callable that returns pages to be displayed.
+# If None a default callable is used (that produces digg-style pagination).
+PAGE_LIST_CALLABLE = getattr(settings, "ENDLESS_PAGINATION_PAGE_LIST_CALLABLE", None)
+
+# Default callable returns pages for digg-style pagination, and depends
+# on the settings below.
+DEFAULT_CALLABLE_EXTREMES = getattr(settings, 
+    "ENDLESS_PAGINATION_DEFAULT_CALLABLE_EXTREMES", 3)
+DEFAULT_CALLABLE_AROUNDS = getattr(settings, 
+    "ENDLESS_PAGINATION_DEFAULT_CALLABLE_AROUNDS", 2)

endless_pagination/templates/endless/current_link.html

+<span class="endelss_page_current"><strong>{{ page.label|safe }}</strong></span>

endless_pagination/templates/endless/page_link.html

+<a class="endless_page_link" href="{{ page.path }}">{{ page.label|safe }}</a>

endless_pagination/templates/endless/show_pages.html

+{% for page in pages %}
+    {{ page|default_if_none:'<span class="endless_separator">...</span>' }}
+{% endfor %}

endless_pagination/templatetags/endless.py

 from django.core.paginator import Paginator, EmptyPage
 from django.http import Http404
 
-from endless_pagination import settings, utils
+from endless_pagination import settings, models, utils
 
 register = template.Library()
 
         }
     # no next page, nothing to see
     return {}
+    
+    
+@register.tag
+def get_pages(parser, token):
+    """
+    Usage::
+    
+        {% get_pages %}
+    
+    This is mostly used for digg-style pagination.
+    This call inserts in the template context a *pages* variable, as a sequence
+    of page links. You can use *pages* in different ways:
+    
+        - just print *pages* and you will get digg-style pagination displayed::
+    
+            {{ pages }}
+            
+        - display pages count::
+        
+            {{ pages|length }}
+            
+        - get a specific page::
+            
+            {# the current selected page #}
+            {{ pages.current }} 
+            
+            {# the first page #}
+            {{ pages.first }} 
+            
+            {# the last page #}
+            {{ pages.last }} 
+            
+            {# the previous page (or nothing if you are on first page) #}
+            {{ pages.previous }} 
+            
+            {# the next page (or nothing if you are in last page) #}
+            {{ pages.next }}
+            
+            {# the third page #}
+            {{ pages.3 }}
+            {# this means page.1 is the same as page.first #}
+            
+        - iterate over *pages* to get all pages::
+        
+            {% for page in pages %}
+                {# display page link #}
+                {{ page }} 
+                
+                {# the page url (beginning with "?") #}
+                {{ page.url }} 
+                
+                {# the page path #}
+                {{ page.path }} 
+                
+                {# the page number #}
+                {{ page.number }} 
+                
+                {# a string representing the page (commonly the page number) #}
+                {{ page.label }}
+                
+                {# check if the page is the current one #}
+                {{ page.is_current }}
+                
+                {# check if the page is the first one #}
+                {{ page.is_first }}
+                
+                {# check if the page is the last one #}
+                {{ page.is_last }} 
+            {% endfor %}
+        
+    You can change the variable name, e.g.::
+    
+        {% get_pages as page_links %}
+    
+    Must be called after {% paginate objects %}.
+    """
+    # args validation
+    try:
+        tag_name, args = token.contents.split(None, 1)
+    except ValueError:
+        var_name = "pages"
+    else:
+        args = args.split()
+        if len(args) == 2 and args[0] == "as":
+            var_name = args[1]
+        else:
+            message = "%r tag invalid arguments" % tag_name
+            raise template.TemplateSyntaxError, message
+            
+    # call the node
+    return GetPagesNode(var_name)
+    
+class GetPagesNode(template.Node):
+    """
+    Insert into context the page list.
+    """
+    def __init__(self, var_name):
+        self.var_name = var_name 
+    
+    def render(self, context):
+        # this can raise a PaginationError 
+        # (you have to call paginate before including the get pages template)
+        page = utils.get_page_from_context(context)
+        # put the PageList instance in the context
+        context[self.var_name] = models.PageList(context["request"], page)
+        return ""
+        
+        
+@register.tag
+def show_pages(parser, token):
+    """
+    Show page links.
+    Usage::
+    
+        {% show_pages %}
+        
+    It is only a shortcut for::
+    
+        {% get_pages %}
+        {{ pages }}
+    
+    You can set *ENDLESS_PAGE_LIST_CALLABLE* in your settings.py as a callable 
+    used to customize the pages that are displayed.
+    The callable takes the current page number and the total number of pages
+    and must return a sequence of page numbers that will be displayed.
+    The sequence can contain other values:
+    
+        - *"previous"*: will display the previous page in that position
+        - *"next"*: will display the next page in that position
+        - *None*: a separator will be displayed in that position
+        
+    Here is an example of custom calable that displays previous page, then
+    first page, then a separator, then current page, then next page::
+    
+        def get_page_numbers(current_page, num_pages):
+            return ("previous", 1, "...", current_page, "next")
+    
+    If *ENDLESS_PAGE_LIST_CALLABLE* is *None* an internal callable is used,
+    generating a digg-style pagination.
+    
+    Must be called after {% paginate objects %}.
+    """
+    # args validation
+    if len(token.contents.split()) != 1:
+        message = "%r tag takes no arguments" % token.contents.split()[0]
+        raise template.TemplateSyntaxError, message
+    # call the node
+    return ShowPagesNode()
+    
+class ShowPagesNode(template.Node):
+    """
+    Show the pagination.
+    """
+    def render(self, context):
+        # this can raise a PaginationError 
+        # (you have to call paginate before including the get pages template)
+        page = utils.get_page_from_context(context)
+        # unicode representation of the sequence of pages
+        return unicode(models.PageList(context["request"], page))

endless_pagination/utils.py

-from endless_pagination.settings import PAGE_LABEL
+from endless_pagination.settings import (PAGE_LABEL, 
+    DEFAULT_CALLABLE_EXTREMES, DEFAULT_CALLABLE_AROUNDS)
 from endless_pagination import exceptions
 
 def get_page_number_from_request(request, page_label=PAGE_LABEL):
     if querydict:
         return "%s%s" % (prefix, querydict.urlencode())
     return ""
-        
-    
+    
+def get_page_numbers(current_page, num_pages, 
+    extremes=DEFAULT_CALLABLE_EXTREMES, arounds=DEFAULT_CALLABLE_AROUNDS):
+    """
+    Default callable for page listing.
+    Produces a digg-style pagination.
+    """
+    page_range = range(1, num_pages+1)
+    pages = ["previous"]
+    
+    # get first and last pages (extremes)
+    first = page_range[:extremes]
+    pages.extend(first)
+    last = page_range[-extremes:]
+    
+    # get current pages (arounds)
+    current_start = current_page - 1 - arounds
+    if current_start < 0:
+        current_start = 0
+    current_end = current_page + arounds
+    if current_end > num_pages:
+        current_end = num_pages
+    current = page_range[current_start:current_end]
+    
+    # mix first with current pages
+    diff = current[0] - first[-1]
+    to_add = current
+    if diff > 1:
+        pages.append(None)
+    elif diff < 1:
+        to_add = current[abs(diff)+1:]
+    pages.extend(to_add)
+    
+    # mix current with last pages
+    diff = last[0] - current[-1]
+    to_add = last
+    if diff > 1:
+        pages.append(None)
+    elif diff < 1:
+        to_add = last[abs(diff)+1:]
+    pages.extend(to_add)
+    
+    pages.append("next")
+    return pages

media/js/endless.js

 $(document).ready(function(){
     // initializes links for ajax requests
-    $(".endless_more").live("click", function(){
+    $("a.endless_more").live("click", function(){
         var container = $(this).closest(".endless_container");
         var loading = container.find(".endless_loading");
         $(this).hide();
         });
         return false;
     });
+    $("a.endless_page_link").live("click", function(){
+        $(this).closest(".endless_page_template").load($(this).attr("href"));
+        return false;
+    }); 
 });
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.