【问题标题】:TemplateDoesNotExist in project folder django 1.8项目文件夹 django 1.8 中的 TemplateDoesNotExist
【发布时间】:2015-06-19 01:04:01
【问题描述】:

我已经构建了我的应用程序 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" %} 有效吗?
  • 您的模板设置是什么?
  • 请出示您的设置文件。或者dpaste它。
  • 这是我的 settings.py:dpaste.com/1W06XKR

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


【解决方案1】:

您需要告诉 Django 您的模板文件夹 (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 元组就会被忽略。
【解决方案2】:

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',
        ],
    },
},]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    相关资源
    最近更新 更多