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. FileField having auto upload_to path by junaidmgithub 15 hours, 24 minutes ago
  2. LazyPrimaryKeyRelatedField by LLyaudet 1 week, 1 day ago
  3. CacheInDictManager by LLyaudet 1 week, 1 day ago
  4. MYSQL Full Text Expression by Bidaya0 1 week, 2 days ago
  5. Custom model manager chaining (Python 3 re-write) by Spotted1270 2 weeks, 1 day 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.