weaveoftheride
weaveoftheride

Reputation: 4395

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

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.

Upvotes: 11

Views: 39207

Answers (6)

bl3ssedc0de
bl3ssedc0de

Reputation: 1015

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,
        ...
    },
]

Upvotes: 2

ninyesiga
ninyesiga

Reputation: 91

Adding this in web/settings.py solved everything for me. Hope it can help you too.

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

Upvotes: -1

BFunk
BFunk

Reputation: 377

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',
            ],
        },
    },
]

Upvotes: 16

buttercup
buttercup

Reputation: 1116

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/'),
)

Upvotes: 1

Transformer
Transformer

Reputation: 3760

the PROJECT_DIR has not been defined... the PROJECT_DIR is not a variable. its a directory/ a path to where the folder "templates" is located. This should help

import os
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = os.path.join(PROJECT_DIR, 'templates')

Upvotes: 3

eminor
eminor

Reputation: 933

You need to specify the absolute path to your template folder. Always use forward slashes, even on Windows.

For example, if your project folder is "/home/djangouser/projects/myproject" (Linux) or 'C:\projects\myproject\' (Windows), your TEMPLATE_DIRS looks like this:

    # for Linux
    TEMPLATE_DIRS = (
        '/home/djangouser/projects/myproject/templates/',
    )

    # or for Windows; use forward slashes!
    TEMPLATE_DIRS = (
        'C:/projects/myproject/templates/',
    )

Alternatively you can use the specified PROJECT_ROOT variable and generate the absolute path by joining it with the relative path to your template folder. This has the advantage that you only need to change your PROJECT_ROOT, if you copy the project to a different location. You need to import the os module to make it work:

# add at the beginning of settings.py
import os

# ...

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

Upvotes: 18

Related Questions