foadster foadster - 7 months ago 25
Python Question

Getting TemplateDoesNotExist from Django 1.8

** I'm using Django 1.8. The templates feature has changed in this release of Django. Read more here Upgrading templates to Django 1.8**

This is bothering me because I've come across this issue and fixed it for one of my other projects, but I can't for the life of me figure out how to fix it this time around. I've gone through countless stackoverflow questions and tried to resolve the issue using the answers provided by I've had no luck. This is the error message I am getting:

Exception Type: TemplateDoesNotExist
Exception Value:
index.html
Exception Location: /Library/Python/2.7/site-packages/django/template/loader.py in get_template, line 46
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/Users/User1/Documents/PyCharmProjects/Project1',


It seems that is it looking in the wrong folder, it should be looking under Project1/templates according to my settings.py file:

import os

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

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

TEMPLATE_DIRS = (
TEMPLATE_PATH,
)

TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
#'django.template.loaders.eggs.load_template_source',
)

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'Project1.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]


My templates folder is in the root folder of my project. What's the issue here? I have given it a TEMPLATE_DIRS parameter, and used a proper BASE_DIR, which is what the majority of the answers recommend.

Answer

remove the slashes: TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')

See here

Things have changed with Django 1.8, in which the template system has been improved. See the release notes. In your settings.py add:

TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [BASE_DIR+"/templates", ],
            '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',
                ],
            },
        },
    ]

the code above comes straight from one of my projects. Feel free to use os.path.join(BASE_DIR, 'templates') instead of catenating the strings.

Comments