我不能将一个html文档(child - base_home.html)扩展到另一个父html文档 - base.html我有下一个Django项目结构:
Testdjango/
blog/
migrations/
templates/
blog/
base.html
base_home.html
__init.py__
admin.py
apps.py
models.py
tests.py
urls.py
views.py
db.sqlite3
manage.py
我的父模板是base.html,带有下一个代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<title>{% block title %}{% endblock %} – Blog</title>
<!-- Bootstrap core CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/journal/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template
<link href="starter-template.css" rel="stylesheet"> -->
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Blog</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<div class="container">
{% block content %}
{% endblock %}
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
我的“孩子”base_home.html有下一个代码:
{% extends "blog/base.html" %}
{% block title %}
Home
{% endblock %}
{% block content %}
Lorem bla bla
{% endblock %}
在页面中我无法从base_home.html中看到任何内容
您的模板似乎没问题,请确保您的模板设置看起来像这样,您也已将应用程序添加到INSTALLED_APPS
。
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',
],
},
},
]
由于base.html
和base_home.html
在同一个dir,扩展基数为,
{% extends "base.html" %}
尝试
{% extends "base.html" %}
代替
{% extends "blog/base.html" %}
您可以通过两种方式扩展模板
base.html文件
include
假设您需要包含一个特殊的div,表格或代码段使用extends
,如果它在文件夹中<html>
<head></head>
<body>
======
======
</body>
</html>
home.html
If base.html in any folder please mention folder name dont put slash(/) before the foldername `{% extends 'foldername/base.html' %}`
{% extends 'base.html' %} ## you can see the base.html styles in the home.html
你有类似的东西吗?
{% include 'base.html' %}
在你的设置?
你有你的博客应用程序在项目设置{% include 'foldername/base.html' %}
注册?
我想我找到了asnwer。在我看来,我有返回渲染(请求,“blog / base.html”)。我chage它返回渲染(请求,“blog / base_home.html”) - 我已经扩展。现在它的工作。请告诉我,这是真的吗?
Django无法识别您尝试结束的块。你需要定义它。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
'DIRS': [
str(ROOT_DIR.path('templates')),
],`...