Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting TemplateDoesNotExist from Django 1.8

Tags:

python

django

** 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.

like image 308
farbodg Avatar asked Dec 10 '22 23:12

farbodg


2 Answers

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.

like image 104
Pynchia Avatar answered Jan 07 '23 00:01

Pynchia


1) If your route for templates is project_name/app_name/templates/app_name

'DIRS': [os.path.join(BASE_DIR, 'app_name', 'templates', 'app_name')],

2) If your route for templates is project _name/templates or project_name/app_name/templates_only

'DIRS': [#leave it just empty, will work fine],

Note: 'template_only' means that templates/*.html there is no any folder inside templates.

like image 24
nturganaliev Avatar answered Jan 07 '23 02:01

nturganaliev