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 pieces of templates


Django also gives us the ability to cache fragments of templates using a special template tag. We can use it to cache the part of the template that evaluates the current time.

Remove the line of code that caches the detail view and reload the page a few times to make sure the time is refreshing each time. Your detail view should look like this again:

def detail(request, pid):
    '''
    Accepts a press release ID and returns the detail page 
    '''
    p = get_object_or_404(PressRelease, id=pid)
    t = loader.get_template('press/cache_detail.html')
    c = Context({'press': p})
    return HttpResponse(t.render(c))

Once you've reverted the view to it's pre-caching state, add the highlighted lines to mycompany/templates/press/detail.html:

<html>
<head>
<title>{{ press.title }}</title>
</head>
<body>
<h1>{{ press.title }}</h1>
<p>
{% load cache %}
{% cache 10 curtime %}
<hr>Current Datetime: {% now "H:m:s...