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

Caching individual views


Now that you've configured caching, let's implement it in one of our views. Our press release detail view is a great candidate for caching. The content of the individual releases rarely changes and, therefore, doesn't need to be queried from the database for every request.

We'll use the same view at the mycompany/press/views.py file that we've been using throughout our chapters. In Chapter 6, we added functionality to the view to work with multiple templates by adding the highlighted lines:

def detail(request, pid):
    '''
    Accepts a press release ID and returns the detail page
    '''
    p = get_object_or_404(PressRelease, id=pid)

    if request.GET.has_key('printable'):
        template_file = 'press/detail_printable.html'
    else:
        template_file = 'press/detail.html'

    t = loader.get_template(template_file)
    c = Context({'press': p})
    return HttpResponse(t.render(c))

Let's simplify the view so that we can just focus on learning how to work...