【问题标题】:Django TemplateDoesNotExist {% extends base.html %} - where should template be?Django TemplateDoesNotExist {% extends base.html %} - 模板应该在哪里?
【发布时间】:2016-11-13 13:37:26
【问题描述】:

我正在启动一个非常简单的 Django 应用程序,但在扩展 html 文件时遇到了问题。

我在my_site/my_app/templates/my_app 中有base.htmlindex.html

my_site/my_app/templates/my_app/base.htmlmy_site/my_app/templates/my_app/index.html

index.html 文件中我有{% extends 'base.html' %}

我的settings.py 文件有

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

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

但是当我在http://127.0.0.1:8000/index/ 访问我的索引视图时:

def index(request):
    return render(request, 'my_app/index.html')

我收到以下错误:

TemplateDoesNotExist at /index/
base.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/index/
Django Version: 1.10.3
Exception Type: TemplateDoesNotExist
Exception Value: base.html

我是否将base.html 文件保存在错误的位置,还是其他原因?我没能解决这个问题。

【问题讨论】:

  • BASE_DIR 是如何定义的?
  • 忘记添加了,现在包含在上面。 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 和 settings.py 保存在 my_site/my_site

标签: python html django django-templates extends


【解决方案1】:

它应该在 my_site/my_app/templates 或 my_site/templates 中。

【讨论】:

  • 我已尝试将其移至 my_site/my_app/templates 但随后出现NoReverseMatch at /index/ 错误:Reverse for 'index' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
  • 嗯,这是一个完全独立的问题,与找不到您的模板无关。
  • 谢谢 - 你是对的。我现在也修复了这个问题,所以一切正常。你的答案已经解决了我的问题 - 现在正在找到模板。
猜你喜欢
  • 1970-01-01
  • 2019-05-15
  • 1970-01-01
  • 2017-12-27
  • 1970-01-01
  • 2016-05-16
  • 1970-01-01
  • 2019-07-12
  • 2016-02-14
相关资源
最近更新 更多