Arseni Baranov
Arseni Baranov

Reputation: 13

TemplateDoesNotExist in django

Hello, I am new to learning django and I am stuck with this TemplateDoesNotExist error

I am running Windows 8

When i check the error page, the value TEMPLATE_DIRS is ['C:/ENVS/boardgames/boardgames/templates'] and this path is completely right. From there I need to load 'helloworld.html' as template

I do this in Views.py like this

from django.views.generic.base import TemplateView
from django.http import HttpResponse


class HelloWorldView(TemplateView):
    template_name='helloworld.html'

Settings.py

"""
Django settings for boardgames project.

Generated by 'django-admin startproject' using Django 1.8.

"""

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

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



# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*r7$d%0eg(^7e&-ad)%(17zv-zm-3m87uroxe+9+e)4&qg4w(a'

# SECURITY WARNING: don't run with debug turned on in production!
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',
    'boardgames',
)

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

WSGI_APPLICATION = 'boardgames.wsgi.application'



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


# Internationalization


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)


STATIC_URL = '/static/'
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates').replace('\\','/')]

and Url.py

from django.conf.urls import include, url
from django.contrib import admin

from .views import HelloWorldView
urlpatterns = [
    # Examples:
     url(r'^$', HelloWorldView.as_view(), name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
]

When I look at the tutorials there are it seems so easy. And it actually works when I copy the templates folder to django\contrib directory. I am really in pain. But I want it done properly and load templates from the directory I want them to be in

What am I doing wrong? Please help

Upvotes: 1

Views: 2659

Answers (1)

catavaran
catavaran

Reputation: 45575

TEMPLATE_DIRS is deprecated in django 1.8. You should use the TEMPLATES setting instead. You already have this variable in your settings.py file so alter it like this:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\','/')],
        '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: 3

Related Questions