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

Working with the template loaders


Django's template loaders ease the burden of working with template files on the filesystem. You just tell the loader which template file to use and the rest is taken care of. Before we look at the actual loaders, let's look at why we want to use them.

Loading templates manually

If you didn't use a loader, you'd have to write a slew of Python code when working with template files. In order to load a file manually, you'd have to:

  • Check for the existence of the template file

  • Check that you have permissions to open the template file

  • Code exception handling if the file doesn't exist or you can't read it

  • Write open(), read(), and close() methods on the file handle every time you want to use it

  • Pass the file contents into the template loader

Not only is this tedious, it's boring, messy, and error-prone. Consider these two code samples:

Without template loader:

try:
    f = open(os.path.join(settings.TEMPLATE_DIRS[0]), 
        'press/demo.html')
    data = f.read()
   ...