Login

Dynamically specify TEMPLATE_DIRS

Author:
ajs17
Posted:
February 28, 2007
Language:
Python
Version:
Pre .96
Score:
-3 (after 3 ratings)

In your site’s settings.py module (in your site root), TEMPLATE_DIRS takes absolute paths. Here is a way to dynamically determine the absolute path to the application directory so you only have to specify relative paths within settings.py. Obviously, replace “application_directory” with the name of your application’s directory.

1
2
3
4
5
import os
dirname = os.path.dirname(globals()["__file__"])
TEMPLATE_DIRS = (
    os.path.join(dirname, 'application_directory/templates'),
)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

whiskybar (on February 28, 2007):

I used to use the exact same code but then I discovered you can achieve the same by

TEMPLATE_LOADERS = (
    "django.template.loaders.app_directories.load_template_source",
)
TEMPLATE_DIRS = ()

#

Please login first before commenting.