- 用url_for加载静态文件
- <script src="{{ url_for('static',filename='js/login.js') }}"></script>
- flask 从static文件夹开始寻找
- 可用于加载css, js, image文件
- 继承和扩展
- 把一些公共的代码放在父模板中,避免每个模板写同样的内容。base.html
- 子模板继承父模板
- {% extends 'base.html’ %}
- 父模板提前定义好子模板可以实现一些自己需求的位置及名称。block
- <title>{% block title %}{% endblock %}-MIS问答平台</title>
- {% block head %}{% endblock %}
- {% block main %}{% endblock %}
- 子模板中写代码实现自己的需求。block
- {% block title %}登录{% endblock %}
- 首页、登录页、注册页都按上述步骤改写。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
{% block title %}
{% endblock %}
广州商学院</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/base.css') }}">
<script src ="{{ url_for('static',filename='js/base.js') }}"></script>
{% block head %}
{% endblock %}
</head>
<body id="myBody">
<nav>
<img id="myOnOff" onclick="mySwitch()" src="https://www.runoob.com/images/pic_bulbon.gif" height="20" width="20px">
<a href="{{ url_for('index') }}">首页</a>
<input type="text" name="search">
<button type="submit">搜索</button>
<a class="right" href="{{ url_for('denglu') }}">登录</a>
<a href="{{ url_for('zhuce') }}">注册</a>
</nav>
{% block main %}
{% endblock %}
<footer>
<div class="footer_box">
Copyright@ 2025个人版权,版权所有
</div>
</footer>
</body>
</html>
{% extends'base.html' %}
{% block title %}
首页
{% endblock %}
{% block head %}
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/index.css') }}">
{% endblock %}
{% block main %}
<body>
<div>
<p><span style="font-size: 40px;color: deeppink;font-family: 'Consolas', 'Deja Vu Sans Mono', 'Bitstream Vera Sans Mono', monospace;">欢迎</span></p>
</div>
<body>
<input type="text">
<button>搜索</button>
<div class="recommend">
<div class="img">
<a href="http://www.gzcc.cn">
<img src="http://www.gzcc.cn/2016/images/yhdh/01.jpg"></a>
<div class="desc">
<a href="http://www.gzcc.cn">学校荣誉</a></div>
</div>
<div class="img">
<a href="http://www.gzcc.cn">
<img src="http://www.gzcc.cn/2016/images/yhdh/05.jpg"></a>
<div class="desc">
<a href="http://www.gzcc.cn">画说校园</a></div>
</div>
<div class="img">
<a href="http://www.gzcc.cn">
<img src="http://www.gzcc.cn/2016/images/yhdh/02.jpg"></a>
<div class="desc">
<a href="http://www.gzcc.cn">师生获奖</a></div>
</div>
<div class="img">
<a href="http://www.gzcc.cn">
<img src="http://www.gzcc.cn/2016/images/yhdh/06.jpg"></a>
<div class="desc">
<a href="http://www.gzcc.cn">视频校园</a></div>
</div>
<div class="img">
<a href="http://www.gzcc.cn">
<img src="http://www.gzcc.cn/2016/images/yhdh/03.jpg"></a>
<div class="desc">
<a href="http://www.gzcc.cn">领导关怀</a></div>
</div>
<div class="img">
<a href="http://www.gzcc.cn">
<img src="http://www.gzcc.cn/2016/images/yhdh/07.jpg"></a>
<div class="desc">
<a href="http://www.gzcc.cn">全景校园</a></div>
</div>
<div class="img">
<a href="http://www.gzcc.cn">
<img src="http://www.gzcc.cn/2016/images/yhdh/08.jpg"></a>
<div class="desc">
<a href="http://www.gzcc.cn">数字广商</a></div>
</div>
<div class="img">
<a href="http://www.gzcc.cn">
<img src="http://www.gzcc.cn/2016/images/yhdh/04.jpg"></a>
<div class="desc">
<a href="http://www.gzcc.cn">校友风采</a></div>
</div>
</div>
</body>
{% endblock %}
{% extends'base.html' %}
{% block title %}登录{% endblock %}
{% block head %}
<link href="../static/css/denglu.css" rel="stylesheet" type="text/css">
<script src="../static/js/denglu.js"></script>
{% endblock %}
{% block main %}
<div class="box">
<h2>欢迎进入</h2>
<h3>登录界面</h3>
<div class="input_box">
<input id="uname" type="text" placeholder="请输入用户名">
</div>
<div class="input_box">
<input id="upass" type="password" placeholder="请输入密码">
</div>
<div id="error_box"><br></div>
<div class="input_box">
<button onclick="myLogin()">登陆</button>
</div>
</div>
{% endblock %}
{% extends 'base.html' %}
{% block title %}注册{% endblock %}
{% block head %}
<link href="../static/css/zhuce.css" rel="stylesheet" type="text/css">
<script src="../static/js/zhuce.js"></script>
{% endblock %}
{% block main %}
<div class="box">
<h2>欢迎进入</h2>
<h3>注册界面</h3>
<div class="input_box">
请输入账号 <input id="uname" type="text" placeholder="请输入你的姓名"> </div><br>
<div class="input_box">
请输入密码 <input id="upass" type="password" placeholder="请输入密码"></div><br>
<div class="input_box">
再输入密码 <input id="upass1" type="password" placeholder="请再次输入密码"></div><br>
<div id="error_box"><br></div>
<div class="input_box">
<button onclick="fnLogin()" >注册</button></div>
</div>
{% endblock %}
![]()
![]()