- 用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 %}
- 首页、登录页、注册页都按上述步骤改写。
1.
<link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/base.css') }}"> <script src="{{ url_for('static',filename='js/base.js') }}"></script> <li id="myBody" style="float: right"> <img id="myOnOff" onclick="mySwitch()" src="{{url_for( 'static',filename='img/on.jpg')}}" width="40px"></li> <li style="float:right"><a href="{{ url_for('login') }}" target="_blank">登录</a></li> <li style="float:right"><a href="{{ url_for('register') }}" target="_blank">注册</a></li>
2.base.html代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %} {% endblock %} baseTest</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> <div class="navbar"> <nav class="navbar-top"> <li class="active"><a href="{{ url_for('index') }}" target="_blank">首页</a></li> <li><a href="#">歌单</a></li> <li><a href="#">电台</a></li> <li><a href="#">音乐人</a></li> <li><input type="text" id="select" placeholder="音乐搜索,找人"></li> <li id="submit"><a href="#">搜索</a></li> <li id="myBody" style="float: right"> <img id="myOnOff" onclick="mySwitch()" src="{{url_for( 'static',filename='img/on.jpg')}}" width="40px"></li> <li style="float:right"><a href="{{ url_for('login') }}" target="_blank">登录</a></li> <li style="float:right"><a href="{{ url_for('register') }}" target="_blank">注册</a></li> </nav> </div> <body id="myBody"> <footer> <div class="row"> <div class="col-sm-12"> <div style="text-align: center;"> <hr> <p>Copyright © 2013-2017<a target="_blank" href="//www.runoob.com/">菜鸟教程</a></p> </div> </div> </div> </footer> {% block main %} {% endblock %} </body> </html>
主页代码:
{% extends 'base.html' %} {% block title %} index {% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/index.css') }}"> {% endblock %} {% block main %} <div class="recommand"> <div class="col-sm-12"> <div class="index-normalNews-header-focus"> <h4 class="normalNewstitle">用户导航</h4> </div> <hr> </div> <div class="img"> <a href="#"><img src="{{ url_for('static',filename='img/test1.jpg') }}"></a> <div class="dese"><a href="#">Test1</a></div> </div> <div class="img"> <a href="#"><img src="{{ url_for('static',filename='img/test2.jpg') }}"></a> <div class="dese"><a href="#">Test2</a></div> </div> <div class="img"> <a href="#"><img src="{{ url_for('static',filename='img/test3.jpg') }}"></a> <div class="dese"><a href="#">Test3</a> </div> </div> <div class="img"> <a href="#"><img src="{{ url_for('static',filename='img/test4.jpg') }}"></a> <div class="dese"><a href="#">Test4</a> </div> </div> </div> <br> {% endblock %}
登录页代码:
{% extends 'base.html' %} {% block title %} login {% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/10.31.css') }}"> <script src="{{ url_for('static',filename='js/10.31.js') }}"></script> {% endblock %} {% block main %} <div class="box"> <div id="title">LoginText</div> <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="fnLogin()">登录</button> <a href="{{ url_for('register') }}">注册/Register</a> </div> </div> {% endblock %}
注册页代码:
{% extends 'base.html' %} {% block title %} Register {% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/10.31.css') }}"> <script src="{{ url_for('static',filename='js/register.js') }}"></script> {% endblock %} {% block main %} <div class="box"> <div id="title">RegisterText</div> <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 class="input-box"> 验证:<input id="upass1" type="password" placeholder="请再次输入密码"> </div> <div id="error-box"><br></div> <div class="input-box"> <button onclick="fnRegister()">注册/Register</button> <a href="{{ url_for('login') }}">已注册/Login</a> </div> </div> {% endblock %}
效果图如下:
主页
登录页
注册页