Practically in any site there is a need to break information output into pages. In Jange, there are already some things that will help in writing your paginator, but to take a pagination for any view in two lines, I haven’t yet seen this
I decided to write one, since the idea of how to make it came to me was born a long time ago. So we meet,
django-simplepagination .
')
A paginator is essentially a decorator that you put on your view with one condition. The condition is very simple; the view should return a dictionary, not a HttpResponse. If you use a samopisny decorator render_to or use one of
django-annoying, then all the conditions for pagination are met. Then everything is very simple, install django-simplepagination, add 'simplepagination' to INSTALLED_APPS and set the decorator over the view. For example:
from simplepagination import paginate
from annoying.decorators import render_to
from blog.models import Post
@render_to ( 'blog_posts.html' )
@paginate (style = 'digg' , per_page =15 )
def list_posts (request):
posts = Post . objects . all()
return { 'object_list' : posts}
Next in the 'blog_posts.html' template you insert {% include paginator.template%}, just do not forget to include the paginator.css file in the base template.
This completes the creation of a digg pagination copy.
The natural question is, how does that of the three lines of code, digg'ovskiy paginator, which is very complex in logic and mind, work? Let me explain, the logic of paginators is described in the so-called backend'ah. The digg backend is already built into the application, it is also an example for those who want to write their backend. In addition to logic, you also need html, of course, it is also embedded in the application, just like css with styles for digg paginator.
Of course, it’s unlikely that your site uses exactly the digg version of the paginator, so I’ll show you how to write your backend, for example, let's take the habrabra plugin.
So the first thing you need to create a class derived from simplepagination.backends.Paginator. The logic itself lies inside the paginate function which must be in your class.
from simplepagination.backends import Paginator
class HabrahabrPaginator (Paginator):
def paginate ( self , frame_size, number_of_pages, current_page):
output = {}
if current_page > 1 :
output[ 'prev' ] = current_page - 1
if current_page < number_of_pages:
output[ 'next' ] = current_page + 1
if current_page + frame_size - 1 < number_of_pages:
output[ 'last' ] = number_of_pages
if current_page - frame_size + 1 > 1 :
output[ 'first' ] = 1
if number_of_pages <= frame_size:
output[ 'pages' ] = range ( 1 , number_of_pages + 1 )
else :
output[ 'pages' ] = range ( max (current_page - frame_size + 1 , 1 ), min (current_page + frame_size, number_of_pages + 1 ))
return output
After the backend is triggered, this output is added to the dictionary that returns the view under the name 'paginator' and, accordingly, goes into your template.
Now it's small,
rip out
html and
css from a habr (I did it for you, at the same time prefixed the names of the classes so that they would not coincide with those that may already be on your site), save html under the name paginator_habrahabr.html and connect the saved css.
The last thing you need to do is add your backend to the backend dictionary. Open your site's settings.py and add the following line (change the path to your class to the one you need).
PAGINATION_BACKENDS = {'habrahabr': 'path.to.HabrahabrPaginator'}
This does not end the application's capabilities, you can use several pagination styles on one site, all things have default settings that can be changed both by the general settings of a certain backend and the parameters passed to the decorator. I advise you to look at the
settings.py file and read the descriptions of the settings.