【问题标题】:TemplateDoesNotExist in djangodjango 中的 TemplateDoesNotExist
【发布时间】:2015-04-24 09:40:23
【问题描述】:

你好,我是学习 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)),
]

当我查看教程时,似乎很容易。当我将模板文件夹复制到 django\contrib 目录时,它实际上可以工作。我真的很痛苦。但我希望它正确完成并从我希望它们所在的目录加载模板

我做错了什么?请帮忙

【问题讨论】:

  • 通常调试页面会告诉您哪些焊点已被搜索到模板。请将其与您的文件结构进行比较!
  • helloworld.html 模板是否存在于该文件夹中?
  • 哪个 django 版本?
  • 是的 helloworld.html 存在,django 1.8

标签: python django templates


【解决方案1】:

TEMPLATE_DIRS 在 django 1.8 中已弃用。您应该改用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',
            ],
        },
    },
]
猜你喜欢
  • 2011-06-13
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 2018-07-17
  • 1970-01-01
  • 2018-09-29
  • 1970-01-01
相关资源
最近更新 更多