Book Image

Django 1.0 Template Development

Book Image

Django 1.0 Template Development

Overview of this book

Table of Contents (17 chapters)
Django 1.0 Template Development
Credits
About the Author
About the Reviewers
Preface
Index

Using pagination in your views


Now that we've used the Django shell to interactively look at the methods and properties of the Paginator and Page objects, let's use that knowledge in an actual view.

Creating the view

In the mycompany/press/views.py file, edit the press_list view function to look like this and add the import statement to load the pagination module:

from django.core.paginator import Paginator

def press_list(request):
    ''' 
    Returns a list of press releases
    ''' 

    pl = PressRelease.objects.all()
    p = Paginator(pl, 10)
    cur_page = p.page(request.GET.get('page', 1))

    t = loader.get_template('press/list.html')
    c = Context({
        'press_list': cur_page.object_list,
        'page_obj': cur_page,
    })
    return HttpResponse(t.render(c))

We're using the Paginator the same way we did in the examples from the Django shell. We pass two items into the template context: press_list and page_obj. press_list. While press_list is a list of objects from the current...