2

我已经构建了我的应用程序 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" %}
4

2 回答 2

16

你需要告诉 Django 你的模板文件夹projekt_folder/template

import os

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

然后DIRSTEMPLATES设置变量中设置:

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',
            ],
        },
    },
]
于 2015-04-13T16:35:34.160 回答
0

Django 3.1.5:您需要告诉 Django 模板引擎在哪里寻找您的 base.html 模板,方法是在 DIRS 中添加它的路径,如下所示:

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',
        ],
    },
},]
于 2021-02-04T18:45:52.277 回答