Fleshed out initial version of the paginate templatetag.
authorfloguy <floguy@7f1efe38-554e-0410-b69d-834cb44da2d5>
Thu, 29 May 2008 03:08:14 +0000 (03:08 +0000)
committerfloguy <floguy@7f1efe38-554e-0410-b69d-834cb44da2d5>
Thu, 29 May 2008 03:08:14 +0000 (03:08 +0000)
git-svn-id: https://django-pagination.googlecode.com/svn/trunk@4 7f1efe38-554e-0410-b69d-834cb44da2d5

pagination/templates/pagination/pagination.html
pagination/templatetags/pagination_tags.py

index 8b13789..ac7ad68 100644 (file)
@@ -1 +1,23 @@
-
+<div class="pagination">
+    {% if page_obj.has_previous %}
+        <a href="?page={{ page_obj.previous_page_number }}">&lsaquo;&lsaquo; previous</a>
+    {% else %}
+        <span class="disabled">&lsaquo;&lsaquo; previous</span>
+    {% endif %}
+    {% for page in pages %}
+        {% if page %}
+            {% ifequal page page_obj.number %}
+                <span class="current">{{ page }}</span>
+            {% else %}
+                <a href="?page={{ page }}{% if q %}&amp;q={{ q }}{% endif %}">{{ page }}</a>
+            {% endifequal %}
+        {% else %}
+        ...
+        {% endif %}
+    {% endfor %}
+    {% if page_obj.has_next %}
+        <a href="?page={{ page_obj.next_page_number }}">next &rsaquo;&rsaquo;</a>
+    {% else %}
+        <span class="disabled">next &rsaquo;&rsaquo;</span>
+    {% endif %}
+</div>
\ No newline at end of file
index 8b13789..0bd9805 100644 (file)
@@ -1 +1,35 @@
+try:
+    set
+except NameError:
+    from sets import Set as set
 
+def paginate(context, window=4):
+    paginator = context['paginator']
+    page_obj = context['page_obj']
+    page_range = paginator.page_range
+    first = set(page_range[:window])
+    last_start = len(page_range)-window
+    if last_start < 0:
+        last_start = 0
+    last = set(page_range[last_start:])
+    current_start = page_obj.number-1-window
+    if current_start < 0:
+        current_start = 0
+    current = set(page_range[current_start:page_obj.number-1+window])
+    pages = []
+    if len(first.intersection(current)) == 0:
+        pages.extend(list(first))
+        pages.append(None)
+        pages.extend(list(current))
+    else:
+        pages.extend(first.union(current))
+    if len(current.intersection(last)) == 0:
+        pages.append(None)
+        pages.extend(list(last))
+    else:
+        pages.extend(list(last.difference(current)))
+    return { 
+        'pages': pages,
+        'page_obj': page_obj,
+        'paginator': paginator,
+    }
\ No newline at end of file