vicg
vicg

Reputation: 1368

TemplateDoesNotExist Error Django, it is looking in the right directory

So I'm getting a template does not exist error when I try to load a template. Here are the directories it is telling me it's looking in:

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/vmgarcia/PycharmProjects/statgrabber/statgrabber/templates/betting/index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/Users/vmgarcia/PycharmProjects/statgrabber/lib/python2.7/site-packages/django/contrib/admin/templates/betting/index.html (File does not exist)
/Users/vmgarcia/PycharmProjects/statgrabber/lib/python2.7/site-packages/django/contrib/auth/templates/betting/index.html (File does not exist)

The first path it looks in is the correct path and the file index.html does exist.

This is my setup:

Here is my code:

settings.py

"""
Django settings for statgrabber project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '2hp-5k(@#3u5!o^%r-&i(flq-3a@85ubpy%kcq&p05z(_j+=67'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

# Project templates
# TEMPLATES = 
TEMPLATE_LOADERS = ('django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader')   
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'),)
# Application definition

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

)
# /statgrabber/statgrabber/templates/betting/index.html
# /Users/vmgarcia/PycharmProjects/statgrabber

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

ROOT_URLCONF = 'statgrabber.urls'

WSGI_APPLICATION = 'statgrabber.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

views.py

from django.shortcuts import render
from django.http import HttpResponse

# site index
def index(request):
    context_dict = {'boldmessage': 'I am bold from the context'}
    return render(request, 'betting/index.html', context_dict)

urls.py

from django.conf.urls import patterns, url
from betting import views

urlpatterns = patterns('', 
    url(r'^$', views.index, name='index'))

Any help would be appreciated. I've spent a reasonable amount of time trying to figure out what was wrong and I couldn't figure it out.

One thing to note is that django didn't automatically generate a TEMPLATE_DIRS and TEMPLATE_LOADERS variable and I had to put it in myself.

Upvotes: 1

Views: 728

Answers (1)

Animesh Sharma
Animesh Sharma

Reputation: 3386

The problem as I see it is that you haven't added statgrabber app to the list of installed apps and the django template engine is unable to access the templates inside that.I can think of two solutions.

1.Either put your templates folder inside the betting app.

2.Add statgrabber app to the list of installed apps.

Upvotes: 1

Related Questions