Snippet List
**Designed to hold a list of pages and page ranges for a book/magazine index.**
A custom model field (and accompanying form field) that saves comma-separated pages and page ranges in human-readable string form. Includes some clean-up code, so that you can add a new page or range at the end of an existing entry, and it will put it in numeric order and combine runs into ranges. So this:
4-33, 43, 45, 60-65, 44, 59
becomes the tidy
4-33, 43-45, 59-65
**NOTE:** If you comment out the raising of the `ValidationError` in the form field's validate() method, it will actually clean up any extraneous characters for you (which could be dangerous, but for me is usually what I want), so even this horrible mess:
;4-33, 46a fads i44 ,p45o
gets cleaned to
4-33, 44-46
*This is the first custom field I've ever written for Django, so may be a little rough but seems to work fine.
- multiple
- stringformat
- range
- pages
- index
This one was adapted from [Page numbers with ... like in Digg](http://djangosnippets.org/snippets/1441/). See that one for more reference.
Digg-like page numbering using inclusion tag.
Usage in template:
`{% load pagination %}
{% pagination yourpage %}`
Inclusion template pagination.html:
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}"><img src="{{ MEDIA_URL }}images/page/cyclenav_l.png" alt="Previous page" /></a>
{% endif %}
{% for pnum in begin %}
<a href="?page={{ pnum }}"{% if page.number == pnum %} class="active"{% endif %}>{{ pnum }}</a>
{% endfor %}
{% if middle %}
<strong>...</strong>
{% for pnum in middle %}
<a href="?page={{ pnum }}"{% if page.number == pnum %} class="active"{% endif %}>{{ pnum }}</a>
{% endfor %}
{% endif %}
{% if end %}
<strong>...</strong>
{% for pnum in end %}
<a href="?page={{ pnum }}"{% if page.number == pnum %} class="active"{% endif %}>{{ pnum }}</a>
{% endfor %}
{% endif %}
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}"><img src="{{ MEDIA_URL }}images/page/cyclenav_l.png" alt="Previous page" /></a>
{% endif %}`
Produces:
previous_img 1 2 ... 4 5 6 7 8 9 10 11 12 ... 17 18 next_img
Or:
1 2 3 4 5 6 7 8 ... 17 18 next_img
Or:
previous_img 1 2 ... 10 11 12 13 14 15 16 17 18 next_img
- tag
- django
- templatetag
- pagination
- digg
- pages
Digg-like page numbering using inclusion tag.
Usage in template:
{% load pagination %}
{% pagination yourpage %}
Inclusion template `pagination.html`:
{% load i18n %}
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}" class="previous">{% trans "previous" %}</a>
{% endif %}
{% for pnum in begin %}
{% ifequal page.number pnum %}
<span class="current">{{ pnum }}</span>
{% else %}
<a href="?page={{ pnum }}">{{ pnum }}</a>
{% endifequal %}
{% endfor %}
{% if middle %}
<span class="continue">...</span>
{% for pnum in middle %}
{% ifequal page.number pnum %}
<span class="current">{{ pnum }}</span>
{% else %}
<a href="?page={{ pnum }}">{{ pnum }}</a>
{% endifequal %}
{% endfor %}
{% endif %}
{% if end %}
<span class="continue">...</span>
{% for pnum in end %}
{% ifequal page.number pnum %}
<span class="current">{{ pnum }}</span>
{% else %}
<a href="?page={{ pnum }}">{{ pnum }}</a>
{% endifequal %}
{% endfor %}
{% endif %}
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}" class="next">{% trans "next" %}</a>
{% endif %}
</span>
</div>
Produces:
previous 1 2 ... 4 5 6 7 **8** 9 10 11 12 ... 17 18 next
Or:
**1** 2 3 4 5 6 7 8 ... 17 18 next
Or:
previous 1 2 ... 10 11 12 13 14 15 16 **17** 18 next
- tag
- django
- templatetag
- pagination
- digg
- pages
3 snippets posted so far.