Django 1.0 Template Development

Scott Newman

eBook: $23.99
Formats: PDF, PacktLib, ePub and Mobi formats
$16.79 save 30%!
Print book: $39.99
$35.99 save 10%!
Print + eBook bundle: $63.98
Includes free access to the book on PacktLib
$39.59 save 38%!
Free Shipping! UK, US, Europe and selected countries in Asia.
This book can also be purchased from:
Overview
Table of Contents
The Author
Reviews
Downloads
  • 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 : 978-1-847195-70-8
Author(s) : Scott Newman
Topics and Technologies : All Books, Open Source, Web Development


Table of Contents

Preface
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
Index
  • 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.

Sample chapters

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

Find your book in our support section to find errata and to download code samples.

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.

Special Offers

PacktLib gives you access to this and 600+ other titles with an annual or monthly subscription.

Annual subscription:

$220.00 per annum

Monthly subscription:

$21.99 per month

Buy 2 eBooks
and Get 50% Off
+
Buy Django 1.0 Template Development with Django 1.1 Testing and Debugging and get 50% off both the eBooks.
Just add both the eBooks to your shopping cart and enter ddjebk in the 'Enter Promotion Code' field. Click 'Add Promotion Code' and the discount will be applied.
View Best Selling eBook offers

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.

Approach

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.

Are there no books available that are right for you at the moment? How about signing up to our newsletter to keep up to date?
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
Resources
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
Sort A-Z