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.
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()
, andclose()
methods on the file handle every time you want to use itPass 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() ...