django中的TemplateDoesNotExist

时间:2015-04-24 09:40:23

标签: python django templates

您好,我是新来学习django而且我遇到了这个TemplateDoesNotExist错误

我正在运行Windows 8

当我检查错误页面时,值TEMPLATE_DIRS是[' C:/ ENVS / boardgames / boardgames / templates']并且此路径完全正确。从那里我需要加载' helloworld.html'作为模板

我在 Views.py 中这样做

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

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

当我查看教程时,它似乎很容易。当我将templates文件夹复制到django \ contrib目录时,它确实有效。我真的很痛苦。但是我希望它能够正确完成并从我想要它们的目录中加载模板

我做错了什么?请帮助

1 个答案:

答案 0 :(得分:3)

django 1.8中不推荐使用

TEMPLATE_DIRS。您应该使用TEMPLATES设置。您已在settings.py文件中包含此变量,因此请按以下方式更改:

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