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

Serving printable pages


One of the easiest approaches we will look at is serving an alternative version of a page based on the presence of a variable in the URL (aka a URL parameter). To serve a printable version of an article, for example, we can add ?printable to the end of the URL.

To make it work, we'll add an extra step in our view to check the URL for this variable. If it exists, we'll load up a printer-friendly template file. If it doesn't exist, we'll load the normal template file.

Start by editing the highlighted lines to the detail function in the mycompany/press/views.py file:

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...