Django模板扩展doens`t打开一个文件

时间:2017-04-04 23:43:32

标签: python django python-3.x django-templates

我正在尝试从其他文件中扩展一个html文件,但它只显示了base.html结果。

这是我的项目文件夹:

-diploma
  -catalogue
    -migrations
    -templates
      -catalogue
        base.html
        header.html
    _init__.py
    admin.py
    apps.py
    models.py
    tests.py
    urls.py
    views.py
  -diploma
    __init__.py
    settings.py
    urls.py
    wsgi.py

settings.py

...

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

INSTALLED_APPS = [
    'catalogue',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

...

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

urls.py

from django.conf.urls import url
from catalogue import views

urlpatterns = [
     url(r'^$', views.index, name='index'),
]

views.py

from django.shortcuts import render

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

base.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% block content %}
    {% endblock %}
</body>
</html>

<强烈> header.html中

{% extends 'catalogue/base.html' %}

{% block content %}
    <h1>Hello</h1>
{% endblock %}

真的,试图找到问题,但无法弄清楚。仅影响base.html中的cghanges

1 个答案:

答案 0 :(得分:0)

尝试放: {%extends'base.html'%} 而不是整个路径