Django 1.8无法加载模板

时间:2015-11-11 11:31:05

标签: python django

我是Python Django Web开发的新手。我正在使用Django 1.8.6。我刚刚开始建立一个项目。在设置文件中,我有以下内容:

import os

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

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

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

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 = 'django_app.urls'

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

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

我在' DIRS'中添加了模板文件夹。部分。但是Django无法从该文件夹加载任何模板。它给出以下错误消息:

Django尝试按以下顺序加载这些模板:

Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
    /home/user/Python_Django/Web_App/DjangoVE/lib/python3.4/site-packages/django/contrib/admin/templates/base.html (File does not exist)

    /home/user/Python_Django/Web_App/DjangoVE/lib/python3.4/site-packages/django/contrib/auth/templates/base.html (File does not exist)

出于测试目的,如果我在' DIRS'中提供模板文件夹的绝对路径。现场,它仍然没有成功。示例:

'DIRS': ['/home/user/Python_Django/Web_App/myApp/templates']

请指出我的错误。

3 个答案:

答案 0 :(得分:2)

确保myapp中包含INSTALLED_APPS。然后app目录加载器应该在myapp/templates

中选择模板

不要在myapp/templates中包含DIRS,它会被app目录加载器选中,因为您已将APP_DIRS设置为True

DIRS设置允许您包含项目范围的目录。在您的情况下,设置

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

允许您从/home/user/Python_Django/Web_App/templates加载模板。

答案 1 :(得分:0)

更改

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

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

这将使模板加载器找到您的文件。

答案 2 :(得分:0)

更改

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

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

这将使模板加载器找到您的文件。

他的回答是正确的,你可以创建一个目录"模板"在你的应用程序! enter image description here

;)谢谢@Sebastian Wozny