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

Context rendering shortcuts


Django makes a few shortcuts available to save us from repetitive, common coding.

Using render_to_response()

Since it's pretty common to create a context, load a template, render the template, and then return the rendered string as an HTTP response, Django provides a shortcut at django.shortcuts.render_to_response to do this quickly.

In your mycompany/press/views.py file, add this line to the top of the file to import the render_to_response function:

from django.shortcuts import render_to_response

In the same file, edit the latest function to look like this:

def latest(request):
    ''' Returns information on the latest press release '''
    p = PressRelease.objects.latest()
    return render_to_response('press/latest.html', {
        'press': p,        
    })

Instead of loading a template file, creating a context, and rendering the template with the context, we simply use the render_to_response function that takes a template as its first argument and context as its...