What is the path for TEMPLATE_DIRS in django settings.py when using virtualenv

Viewed 34892

I am using virtualenv and I want to know what the TEMPLATE_DIRS in settings.py should be, for example if I make a templates folder in the root of my project folder.

6 Answers

If you're working with a newer version of Django you may have to add it to the DIR list that is inside settings.py under TEMPLATES.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['[project name]/templates'], # Replace with your project name
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

If you are using Django 1.9, it is recommended to use BASE_DIR instead of PROJECT_DIR.

# add at the beginning of settings.py
import os

# ...

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates/'),
)

TEMPLATE_DIRS deprecated This setting is deprecated since Django version 1.8.

deprecated

""" settings.py """

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates/'),
)

correct

""" settings.py """

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [ os.path.join(BASE_DIR, 'templates') ],
        'APP_DIRS': True,
        ...
    },
]
Related