Django 1.0 Template Development

Django 1.0 Template Development
eBook: $23.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 30%!
Print + free eBook + free PacktLib access to the book: $63.98    Print cover: $39.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters
  • Dive into Django's template system and build your own template
  • Learn to use built-in tags and filters in Django 1.0
  • Practical tips for project setup and template structure
  • Use template techniques to improve your application's performance

Book Details

Language : English
Paperback : 272 pages [ 235mm x 191mm ]
Release Date : December 2008
ISBN : 1847195709
ISBN 13 : 9781847195708
Author(s) : Scott Newman
Topics and Technologies : All Books, Web Development, Open Source, Web Development

Table of Contents

Chapter 1: An Introduction to the Django Template System
Chapter 2: Views, URLs, and Generic Views
Chapter 3: Template Context
Chapter 4: Using the Built-In Tags and Filters
Chapter 5: Loading and Inheriting Templates
Chapter 6: Serving Multiple Templates
Chapter 7: Custom Tags and Filters
Chapter 8: Pagination
Chapter 9: Customizing the Admin Look and Feel
Chapter 10: Caching Your Pages
Chapter 11: Internationalization
  • Chapter 1: An Introduction to the Django Template System
    • What are templates?
      • Understanding the need for templates
    • Overview of the Django template system
      • Separating code from presentation
      • Helping designers and developers collaborate
      • Increasing maintainability
      • Template syntax
      • Modularity and reusability
      • Flexibility
      • Limitations
      • Critics of the system
    • Exploring how Django handles requests
    • Understanding the template system syntax
      • Context variable
      • Variables
      • Filters
      • Tags
      • Comments
      • Code note: Python dictionaries
      • How invalid variables are handled
    • Creating our demo application
      • Starting our application
      • Adding templates to our application
        • Adding variables to the view
        • Moving the logic into a separate template file
        • Using template filters
        • Using template tags to perform logical tests
        • Adding comments
    • Summary
  • Chapter 2: Views, URLs, and Generic Views
    • An overview
    • Creating the application
      • Create the data model
      • Create the admin file
      • Configure the URLs
      • Add data in the admin application
    • Mapping URLs to views
      • Handling unmatched URL patterns
      • Splitting up the URL configurations
    • Creating views
      • Accepting the request object
      • Responding with an HTTP response
      • Responding with an exception
    • Putting the views together
      • Building the basic view
      • Cleaning up the error handling
      • Adding the template files
      • Adding the template to the view
      • Creating the list view and template
        • Using generic views to shorten development time
      • Simple generic views
        • Loading a template directly
        • Redirecting URLs
      • List/detail generic views
        • Replacing the list view
        • Replacing the detail view
      • Using the other generic views
    • Comparing views and generic views
    • Summary
  • Chapter 3: Template Context
    • The context explained
      • Practicing working with the context
      • Using locals for prototyping
      • Adding, changing, and removing items in the context
    • Using the context values in your templates
      • Preventing method execution from templates
    • Handling invalid context variables
    • Cleaning up the view
  • Context rendering shortcuts
    • Using render_to_response()
    • Using render_to_string()
  • Context processors
    • Exploring the default context processors
      • Auth
      • Debug
      • Media
    • il8n
    • Configuring your project to use context processors
    • Configuring your views to use context processors
    • Using render_to_response with RequestContext
    • Using the context processors in our project
    • Writing your own context processor
  • Summary
  • Chapter 4: Using the Built-In Tags and Filters
    • Built-in filter reference
      • add
      • addslashes
      • capfirst
      • center
      • cut
      • date
      • default
      • default_if_none
      • dictsort
      • dictsortreversed
      • divisibleby
      • escape
      • escapejs
      • filesizeformat
      • first
      • fix_ampersands
      • floatformat
      • force_escape
      • get_digit
      • iriencode
      • join
      • last
      • length
      • length_is
      • linebreaks
      • linebreaksbr
      • linenumbers
      • ljust
      • lower
      • make_list
      • phone2numeric
      • pluralize
      • pprint
      • random
      • removetags
      • rjust
      • safe
      • slice
      • slugify
      • stringformat
      • striptags
      • time
      • timesince
      • timeuntil
      • title
      • truncatewords
      • truncatewords_html
      • unordered_list
      • upper
      • urlencode
      • urlize
      • urlizetrunc
      • wordcount
      • wordwrap
      • yesno
    • Built-in tag reference
      • autoescape
      • block
      • comment
      • cycle
      • debug
      • extends
      • filter
      • firstof
      • for
        • forloop
      • if
      • ifchanged
      • ifequal
      • ifnotequal
      • include
      • load
      • now
      • regroup
      • spaceless
      • ssi
      • templatetag
      • url
      • widthratio
      • with
    • Summary
  • Chapter 5: Loading and Inheriting Templates
    • Configuring the template system
    • Finding a home for the template files
    • Working with the template loaders
      • Loading templates manually
      • Choosing a template loader
      • Using the filesystem loader
      • Using the application directories loader
      • About the eggs template loader
      • Using the loaders together
      • Loading your template files
    • Setting up the error handling templates
      • Creating the error templates
      • Testing the error templates
    • Breaking templates into reusable pieces
    • Extending templates with inheritance
      • Using the block tag
      • Extending templates
      • Adding inheritance to the press application
      • Using multiple block tags
      • Adding template inheritance to our press release list
    • Inheriting from multiple child templates
    • Appending to blocks
    • Template strategy
      • Creating content placeholders
        • Extra JS
        • Extra style
        • Extra head content
        • Extra body tag attributes
    • Using include files
      • Using include
        • Using SSI
    • Summary
  • Chapter 6: Serving Multiple Templates
    • Considering the different approaches
      • Serving mobile devices
      • Adapting content
    • Setting up our example
    • Serving printable pages
    • Creating site themes
      • Testing the template overrides
    • Serving different templates by domain name
      • Serving different sites with the development web server
    • Redirecting users to the mobile site (optional)
      • Detecting mobile devices
      • Writing the middleware
      • Checking only once
      • Installing the middleware
    • Summary
  • Chapter 7: Custom Tags and Filters
    • Examining the built-in tags and filters
      • Template filters
      • Template tags
    • Writing your own template filters
      • Setting up a test application
      • Creating a home for our filter library
      • Template filter syntax
      • Loading template libraries
      • U.S. currency filter
      • Replace profanities filter
      • Filters that expect strings
      • In-list filter
    • Writing your own template tags
      • Creating another sample application
      • Adding the template library
      • Template tag syntax
      • A simple tag example
      • The compilation function
      • The template node subclass
      • Registering our custom tag
      • All work and no play tag
      • Passing a template variable to a tag
      • Modifying the context through a tag
    • Summary
  • Chapter 8: Pagination
    • An Overview
    • Verifying our application setup
      • Verifying the application
      • Verifying the configuration
      • Verifying the URL configuration
      • Verifying the model
      • Verifying the view
      • Adding test records
    • Exploring pagination using the Django shell
      • Examining database performance
      • Allowing for empty result sets
      • Preventing orphaned records
    • Using pagination in your views
      • Creating the view
      • Retrieving the current position from the URL
    • Putting navigation into the templates
    • Pagination with generic views
      • Setting up our generic list view
      • Generically calling the last page
    • Summary
  • Chapter 9: Customizing the Admin Look and Feel
    • Overriding the admin templates
      • Leveraging the template loader
      • Locating the admin template files
      • Exploring the admin template files
        • Inspecting the base.html template
        • Inspecting the base_site.html template
        • Inspecting the index.html template
        • Inspecting the change_list.html template
        • Inspecting the change_form.html template
    • Customizing the admin header
      • Replacing the page title
      • Changing the header text
    • Adding a new link box to the admin
      • Overriding the admin index file
      • Creating the include file
    • Customizing the admin color scheme
      • Identifying styles to change
      • Using the extrastyle block
    • Summary
  • Chapter 10: Caching Your Pages
    • An overview
      • Do you need caching?
      • How caching works
    • Exploring the available cache systems
      • Filesystem caching
      • Database caching
      • Memcached
      • Local memory caching
      • Dummy caching
    • Setting up your cache system
      • Configuring the cache backend
        • Database caching
        • Filesystem caching
        • Local memory caching
        • Dummy caching
        • Memcached
      • Adding additional backend arguments
      • Setting up for the examples
    • Caching individual views
      • Adding caching
    • Caching pieces of templates
    • Low-level caching
    • Caching your whole site
      • Preventing data from being cached
    • General caching strategies
    • Working with outside caches
    • Summary
  • Chapter 11: Internationalization
    • Exploring i18n
    • Creating an example application
    • Configuring your project for i18n
    • Installing libraries for i18n translation
    • Marking strings as translatable
    • Creating message files
    • Enabling automatic language preference
      • How Django determines language preference
    • Summary

Scott Newman

Scott Newman has been developing commercial websites since 1997. Since then, he has professionally developed web applications in C, PERL, ColdFusion, ASP, PHP, and Python. He has also been a Windows network administrator and desktop application developer, but always gravitates back to web development. Scott holds a Network+ certification and is a dotMobi Certified Mobile Web Developer.

In recent years, Scott worked as the system development manager for a major media company developing CMS and mobile applications in Django. He is currently the consulting director for the Big Nerd Ranch in Atlanta, GA.

Code Downloads

Download the code and support files for this book.

Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


- 7 submitted: last submission 11 Nov 2013

Errata type: Code | Page number: 95

The example for the urlencode filter uses 'urlize' instead of 'urlencode' The line should read: Apply the filter: {{ myvalue|urlencode }}


Errata type: Code | Page number: 45

'template': 'robots.txt'}), is missing a brace and should read {'template': 'robots.txt'}),


Errata type: Code | Page number: 63

The first sentence of the first section references django.shortcuts.render_to_string, which is incorrect. The render_to_string function is found at django.template.loader.render_to_string. The sentence should read: Django also provides a shortcut at django.template.loader.render_to_string to render the template...


Errata type: Code | Page number: 45

The line above the image should should read : Point your browser to the URL http://localhost:8000/robots/txt. instead of: Point your browser to the URL http://localhost:8000/robots.txt/.


Errata type: Typo | Page number: 195

In the 'Warning', it should be 'get_list_or_404', not 'get_object_or_404'


Errata type: Code | Page number: 170

Half way down the page, it says: "We added the {% load filtertag_extras %} line..." It should be: "We added the {% load filtertest_extras %} line..."


Errata type: Typo | Page number: 219

Last paragraph: "the links will have been worked out" should be "the kinks will have been worked out"


Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

Django 1.0 Template Development +    HTML5 for Flash Developers =
50% Off
the second eBook
Price for both: €27.35

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • Meet the Django template system and understand how it works
  • Understand URL configurations, views, and generic views and streamline your project's development
  • Master built-in tags and filters to use them efficiently in your project
  • Split your output into manageable pieces using pagination libraries
  • Extend your templates through inheritance
  • Customize the look of the admin interface as you like to administer your website with ease
  • Create and deploy custom tags and filters according to your needs
  • Internationalize your templates to present your site in multiple languages from the same template files
  • Improve your site's performance by using the cache framework
  • Serve multiple sets of templates from the same project easily

Chapter 1 gives you an introduction to the Django template system and provides an overview of how it works.

Chapter 2 explores how URL configuration routes your requests to views and generic views. You will understand how to use generic views to streamline your project's development.

Chapter 3 explains how data from your views is exposed to the template system via the template context.

Chapter 4 reviews all of Django's built-in tags and filters, each with examples and usage notes.

Chapter 5 uses extension and inheritance to create a modular skeleton for your project's templates.

Chapter 6 teaches you how to serve multiple versions of your templates in a single Django project. You will create mobile and traditional templates and learn how to serve them from the same views.

Chapter 7 teaches you how to use Django's pagination libraries to split the output of your applications into pages and provide navigation between them.

Chapter 8 explains how to extend the template system by writing your own template tags and filters.

Chapter 9 uses internationalization to automatically serve your site templates in multiple languages based on the user's preferences.

Chapter 10 shows you how to customize the look and feel of the automatic admin application by editing some templates and creating custom CSS rules.

Chapter 11 teaches you to use the cache framework to optimize the speed and performance of your project.

In Detail

Django is a high-level Python web application framework designed to support the rapid development of dynamic websites, web applications, and web services. Getting the most out of its template system allows programmers and designers to easily and efficiently output their content in a flexible, extendable, and maintainable manner.

This book will help you to master the Django template system. Built-in template tags and filters are explained with examples and usage notes, as well as information on building custom tags and filters to extend the system for your needs. You will learn to use inheritance to create modular templates that are easy to maintain. You will learn how to serve multiple templates from within the same Django project, with an example of how to serve different templates for a mobile version of your site without having to change any code in your views. Pagination, internationalization, caching, and customization of the automatic admin application are also covered.

A comprehensive, practical exploration of the usage and customization of Django's template system, including tutorials on pagination, caching, and internationalization.


This book is designed for readers who learn by doing and employs many examples and screenshots to let the reader dig in and start coding. This book isn't designed to be a reference; instead it has a practical, example-driven approach that teaches you by following along with the examples in the chapters.

When you have completed this book, you will fully understand how the template system works, how to extend it when you have specialized needs, and how to optimize the performance and usability of your content.

Who this book is for

This book is for web developers and template authors who want to fully understand and utilize the Django template system. The reader should have completed the introductory tutorials on the Django project's website and some experience with the framework will be very helpful. Basic knowledge of Python and HTML is assumed.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software