在Django 1.8项目文件夹中,模板不存在。

3

我已经按照下面的方式构建了我的Django应用程序(Django 1.8)。

当我尝试在app1或app2中的模板中使用我的应用程序的base.html扩展base.html时,我会收到以下错误信息。

TemplateDoesNotExist at /
base.html
Error during template rendering

In template /myProject/project_folder/app1/templates/app1/base.html, error at line 1
{% extends "base.html" %}

这是我的项目结构:

/projekt_folder
    template
        base.html
    /app1
        /template
            base.html <-- {% extends "base.html" %}
    /app2
        /template
            base.html <-- {% extends "base.html" %}

{% extends "template/base.html" %} 运行正常吗? - user4759923
您的TEMPLATES设置是什么? - user4759923
请展示你的设置文件。或者将其上传到 dpaste - Aamir Rind
这是我的settings.py文件:http://dpaste.com/1W06XKR - mark
2个回答

16

你需要告诉Django你的模板文件夹(projekt_folder/template)的额外位置,该文件夹不在已安装应用程序之下。请在设置文件的顶部添加以下行:


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['projekt_folder/template'],
        # ...
    },
]
import os

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

然后在TEMPLATES设置变量中设置DIRS

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

令人沮丧的是,一旦你有了TEMPLATES列表,TEMPLATE_DIRS元组就会被默默忽略。 - Bob Stein

0
Django 3.1.5: 您需要告诉Django模板引擎在DIRS中添加base.html模板的路径,例如:
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
   
    # Build paths inside the project like this: BASE_DIR / 'subdir'.
    # BASE_DIR in your case is '/projekt_folder'
    'DIRS': [
        BASE_DIR / 'template'
    ],
    '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',
        ],
    },
},]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接