在此之前,感谢所有可以提供帮助的人。我对 Django“模板继承”有一些奇怪的行为 - 主要问题是 (menu.html) 中的 {% extends 'base.html' %} 在目标 (base.html) 文档中不显示任何内容。而相反 - Django 从 base.html 中获取标头并将其加载到 (menu.html) 中,这很奇怪,考虑到继承的逻辑。
我已经阅读了所有类似的问题,尝试了不同的方法,但什么都没有......无论如何 - “包含”标签与所有模板完美配合。
我在 Windows 10 上有 Django 1.11.11 和 python 3.6.4。
让我列出文件本身:
菜单.html:
{% extends 'base.html' %}
{% block title %}
Some text
{% endblock %}
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% block title %}
{% endblock %}
</body>
</html>
网址.py
from django.conf.urls import url
from django.contrib import admin
from linguistic import views
from linguistic.views import index, menu, about, contact
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index),
url(r'^menu/$', views.menu),
url(r'^about/$', views.about),
url(r'^contact/$', views.contact),
]
视图.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'base.html', {'foo':'bar'})
def about(request):
return render(request, 'about.html')
def contact(request):
return render(request, 'contact.html')
def menu(request):
return render(request, 'menu.html', {'foo':'bar'})
Settings.py元素
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
ROOT_URLCONF = 'linguistic.urls'
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',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
项目目录结构:
Django_111:
--Include
--Lib
--linguistic
-----setting.py
-----urls.py
-----views.py
--Scripts
--tcl
--templates
-----base.html
-----about.html
-----menu.html
-----contact.html
--manage.py