一、什么情况下需要使用继承?
附加html脚本:我们可以复制下面的代码到学生管理对应的html中,然后修改content部分;但是过于繁琐!所以需要使用继承!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
body,html{
height: 100%;
width: 100%;
}
.outer{display: flex;flex-direction: column;height: 100%}
.middle{flex: 1;display: flex; background-color: white}
.content{flex: 1; background: aliceblue;}
.nav{
display: flex;
line-height: 40px;
width: 100%;
background-color: cadetblue;
color: white;
font-size: 24px;
justify-content: center;
}
.left {
width: 13%;
background: cornflowerblue;
overflow: auto;
background-color: lightgrey;
}
.manage{
display: flex;
flex-direction: column;
width:100%;
height: 10%;
background-color: lightseagreen;
justify-content: center;
align-items: center;
margin-top: 10px;
font-size:18px ;
font-family: "Times New Roman";
}
a{
text-decoration: none;
}
.manage:hover{
background-color: darkcyan;
font-size: 20px;
}
h1{
text-align: center;
}
</style>
</head>
<body>
<div class="outer">
<div class="nav">标题</div>
<div class="middle">
<div class="left">
<div class="student manage"><a href="/student/">学生管理</a></div>
<div class="teacher manage"><a href="#">老师管理</a></div>
<div class="course manage"><a href="#">课程管理</a></div>
<div class="classes manage"><a href="#">课程管理</a></div>
</div>
<div class="content">
<h1>WELCOME TO maoniuchuxing!!!</h1>
</div>
</div>
</div>
</body>
</html>
二、extend
为了避免复制同样的html脚本,我们只需要将需要变动的内容设置为一个格子:
{% block 格子名称%}
……各种html标签内容……
{%endblock%}
然后,在下一个跳转页html文件中,继承,并修改格子内容为该页面要线上的内容;
{% extends "base.html" %}
{% block content %}
{% for student in student_list %}
<h2>学生{{ student }}</h2>
{% endfor %}
{% endblock %}
截图说明:
1.创建一个基础页面:
2.其他html页面来继承:
3.extend使用说明:
{%extends%}在模板中使用,必须放在第一行,否则无效;
基础模板中的{%block%}标签越多越好,这样后面的继承就会更灵活;
不允许在同一个模板中定义多个同名的{%block%}