Question

I am reading http://www.djangobook.com/en/2.0/chapter04.html which follows Django 1.4 but I use Django 1.6 so how to set the template directory in Django 1.6 as settings.py doesn’t have TEMPLATE_DIRS variable and why the developers changed this? Thanks in advance.

Was it helpful?

Solution

Add to settings.py

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

OTHER TIPS

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

Add this to settings.py. In django 1.6 BASE_DIR is defined. Otherwise define BASE_DIR as

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

According to Django tutorial, you should add TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')] to your settings.py file (so it is a list not a tuple)

It should be

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

Or you might see an error like this :

DeprecationWarning: The TEMPLATE_DIRS setting must be a tuple. Please fix your settings, as auto-correction is now deprecated.
self._wrapped = Settings(settings_module)

For django >= 1.6 it is a tuple

Use the below given code snippet. Paste it in last of the settings.py file.

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

Here BASE_DIR means your project directory, not the inner directory where the settings.py resides. Create a directory named "templates" (without quotes) inside the BASE_DIR and store your templates inside that directory. Django will join templates directory to the BASE_DIR using os.path.join() function. Hope this helps.

As I posted https://stackoverflow.com/a/40145444/6333418 you 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',
            ],
        },
    },
]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top