Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per page #36

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
django-pagination
=================

A set of utilities for creating robust pagination tools throughout a django application.

This modification includes paginate_by tag for multi choice objects per page.

Installation:
=================
In 'settings.py' add following lines (if they aren't already added):

```python
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.media",
"django.core.context_processors.request",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.static",
)
```

In the MIDDLEWARE_CLASSES add ```'pagination.middleware.PaginationMiddleware'```

And in the INSTALLED_APPS add ```'pagination'```


Example:
=================
```{% perpageselect 10 20 30 %}``` will show dropdown list with choices 10, 20 and 30 objects per page.
```{% perpageanchors 10 20 30 %}``` will show anchors with choices 10, 20 and 30 objects per page.


Usage:
=================
```
{% load pagination_tags %}

{% autopaginate object_list 10 %} <!--OR THIS {% perpageselect 10 20 30 %} -->
{% for post in object_list %}
bla-bla-bla
{% endfor %}
{% paginate %}
```
Binary file added pagination/locale/uk/LC_MESSAGES/django.mo
Binary file not shown.
25 changes: 25 additions & 0 deletions pagination/locale/uk/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# django-pagination Ukrainian translation.
# Copyright (C) 2012, Maxym Lynnyk
# This file is distributed under the WTFPL
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-08-29 16:50+0200\n"
"PO-Revision-Date: 2012-08-29 16:50+0200\n"
"Last-Translator: Maxym Lynnyk \n"
"Language-Team: TIPS \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: templates/pagination/pagination.html:5
#: templates/pagination/pagination.html:7
msgid "previous"
msgstr "попередня"

#: templates/pagination/pagination.html:21
#: templates/pagination/pagination.html:23
msgid "next"
msgstr "наступна"
22 changes: 21 additions & 1 deletion pagination/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from django.conf import settings

DEFAULT_PAGINATION = getattr(settings, 'PAGINATION_DEFAULT_PAGINATION', 20)


def get_page(self):
"""
A function which will be monkeypatched onto the request to get the current
Expand All @@ -7,11 +12,26 @@ def get_page(self):
return int(self.REQUEST['page'])
except (KeyError, ValueError, TypeError):
return 1


def get_perpage(self):
try:
self.session['perpage'] = int(self.REQUEST['perpage'])
return self.session['perpage']
except (KeyError, ValueError, TypeError):
pass

try:
return int(self.session['perpage'])
except (KeyError, ValueError, TypeError):
return DEFAULT_PAGINATION


class PaginationMiddleware(object):
"""
Inserts a variable representing the current page onto the request object if
it exists in either **GET** or **POST** portions of the request.
"""
def process_request(self, request):
request.__class__.page = property(get_page)
request.__class__.page = property(get_page)
request.__class__.perpage = property(get_perpage)
12 changes: 12 additions & 0 deletions pagination/templates/pagination/perpageanchors.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% load i18n %}
<div class="perpageanchors">
{% for choice in choices %}
<span>
{% if perpage == choice %}
{% if choice == 0 %}{% trans "All" %}{% else %}{{choice}}{% endif %}
{% else %}
<a href="?perpage={{choice}}">{% if choice == 0 %}{% trans "All" %}{% else %}{{choice}}{% endif %}</a>
{% endif %}
</span>
{% endfor %}
</div>
11 changes: 11 additions & 0 deletions pagination/templates/pagination/perpageselect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% load i18n %}
<div class="perpageselect">
<form action="." method="GET" name="perpage" >
<select name="perpage">
{% for choice in choices %}
<option value="{{choice}}" {% if perpage = choice %} selected="selected" {% endif %}>{% if choice == 0 %}{% trans "All" %}{% else %}{{choice}}{% endif %}</option>
{% endfor %}
</select>
<input type="submit" value="{% trans 'Select' %}" />
</form>
</div>
47 changes: 38 additions & 9 deletions pagination/templatetags/pagination_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from django import template
from django.http import Http404
from django.core.paginator import Paginator, InvalidPage
from django.core.paginator import Paginator, InvalidPage
from django.conf import settings

register = template.Library()
Expand Down Expand Up @@ -51,7 +51,7 @@ def do_autopaginate(parser, token):
else:
raise template.TemplateSyntaxError('%r tag takes one required ' +
'argument and one optional argument' % split[0])

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all whitespace

class AutoPaginateNode(template.Node):
"""
Emits the required objects to allow for Digg-style pagination.
Expand All @@ -69,10 +69,10 @@ class AutoPaginateNode(template.Node):
tag. If you choose not to use *{% paginate %}*, make sure to display the
list of available pages, or else the application may seem to be buggy.
"""
def __init__(self, queryset_var, paginate_by=DEFAULT_PAGINATION,
def __init__(self, queryset_var, paginate_by=None,
orphans=DEFAULT_ORPHANS, context_var=None):
self.queryset_var = template.Variable(queryset_var)
if isinstance(paginate_by, int):
if (paginate_by == None):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be:

if paginate_by is None:

self.paginate_by = paginate_by
else:
self.paginate_by = template.Variable(paginate_by)
Expand All @@ -82,10 +82,13 @@ def __init__(self, queryset_var, paginate_by=DEFAULT_PAGINATION,
def render(self, context):
key = self.queryset_var.var
value = self.queryset_var.resolve(context)
if isinstance(self.paginate_by, int):
paginate_by = self.paginate_by
if (self.paginate_by == None):
paginate_by = int(context['request'].perpage)
else:
paginate_by = self.paginate_by.resolve(context)
if (paginate_by == 0):
context['page_obj'] = value
return u''
paginator = Paginator(value, paginate_by, self.orphans)
try:
page_obj = paginator.page(context['request'].page)
Expand All @@ -99,7 +102,7 @@ def render(self, context):
if self.context_var is not None:
context[self.context_var] = page_obj.object_list
else:
context[key] = page_obj.object_list
context[key] = page_obj.object_list
context['paginator'] = paginator
context['page_obj'] = page_obj
return u''
Expand Down Expand Up @@ -222,9 +225,35 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''):
else:
to_return['getvars'] = ''
return to_return
except KeyError, AttributeError:
except (KeyError, AttributeError):
return {}

register.inclusion_tag('pagination/pagination.html', takes_context=True)(
paginate)
register.tag('autopaginate', do_autopaginate)


@register.inclusion_tag('pagination/perpageselect.html', takes_context='True')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be:

@register.inclusion_tag('pagination/perpageselect.html', takes_context=True)

def perpageselect (context, *args):
"""
Reads the arguments to the perpageselect tag and formats them correctly.
"""
try:
choices = [int(x) for x in args]
perpage = int(context['request'].perpage)
return {'choices': choices, 'perpage': perpage}
except(TypeError, ValueError):
raise template.TemplateSyntaxError(u'Got %s, but expected integer.' % args)


@register.inclusion_tag('pagination/perpageanchors.html', takes_context='True')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be:

@register.inclusion_tag('pagination/perpageselect.html', takes_context=True)

def perpageanchors (context, *args):
"""
Reads the arguments to the perpageanchors tag and formats them correctly.
"""
try:
choices = [int(x) for x in args]
perpage = int(context['request'].perpage)
return {'choices': choices, 'perpage': perpage}
except(TypeError, ValueError):
raise template.TemplateSyntaxError(u'Got %s, but expected integer.' % args)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = '1.0.7'
version = '1.0.8'

LONG_DESCRIPTION = """
How to use django-pagination
Expand Down