Django TemplateDoesNotExist {%extends base.html%} - 模板应该在哪里?

时间:2016-11-13 13:37:26

标签: python html django django-templates extends

我正在开始一个非常简单的Django应用程序但是在扩展html文件方面遇到了麻烦。

base.html内有index.htmlmy_site/my_app/templates/my_app

即。 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文件保存在错误的位置,还是其他内容?我无法解决这个问题。

1 个答案:

答案 0 :(得分:1)

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