项目文件夹django 1.8中的TemplateDoesNotExist

时间:2015-04-13 16:12:25

标签: python django django-templates django-views django-1.8

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

2 个答案:

答案 0 :(得分:16)

您需要告诉Django模板文件夹(projekt_folder/template)的附加位置是什么,它不在已安装的应用程序下,在设置文件的顶部添加以下行:

import os

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

然后在DIRS设置var:

中设置TEMPLATES
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',
            ],
        },
    },
]

答案 1 :(得分:0)

Django 3.1.5: 您需要通过在 DIRS 中添加它的路径来告诉 Django 模板引擎在哪里查找 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',
        ],
    },
},]
相关问题