加载静态文件,父模板的继承和扩展
Posted 因陀罗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加载静态文件,父模板的继承和扩展相关的知识,希望对你有一定的参考价值。
- 用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 %}
首页、登录页、注册页都按上述步骤改写。
from flask import Flask,render_template app = Flask(__name__) @app.route(‘/‘) def shouye(): return render_template(‘base.html‘) #跳转首页 @app.route(‘/denglu/‘) def gg(): return render_template(‘denglu.html‘) #跳转登录 @app.route(‘/zhuche/‘) def login(): return render_template(‘zhuce.html‘)#跳转注册 @app.route(‘/tupian/‘) def hh(): return render_template(‘22c.html‘) #跳转图片库 if __name__ == ‘__main__‘: app.run(debug=True)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> 首页 {% block denglutitle %}{% endblock %} {% block zhucetitle %}{% endblock %} {% block tupiantitle %}{% endblock %} </title> <link rel="stylesheet" type="text/css" href="../static/css/22c.css"> <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="../static/js/mm.js"></script> <script src="../static/js/ww.js"></script> <script src="../static/js/daohang.js"></script> {% block dengluhead %}{% endblock %} {% block zhucehead %}{% endblock %} {% block tupianhead %}{% endblock %} </head> <body id="mybody"> <nav class="navbar navbar-inverse" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">首页</a> </div> <div> <ul class="nav navbar-nav"> <li><a href="{{ url_for(‘gg‘) }}" onclick="">登陆</a></li> <li><a href="{{ url_for(‘login‘) }}" onclick="">注册</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 设置 <b class="caret"></b> </a> <ul class="dropdown-menu"> <li><a href="#">收藏</a></li> <li><a href="#">分享</a></li> <li><a href="#">搜索</a></li> <li class="divider"></li> <li><a href="#">点赞</a></li> </ul> </li> <li><a href="#">||</a></li> </ul> </div> <div style="float: left"> <img id="myonoff" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" style="width:40px" > </div> <div> <ul class="nav navbar-nav"> <li><a href="{{ url_for(‘hh‘) }}" onclick="">图片区</a></li> </ul> </div> <div> <input type="text" name="user" id="user" placeholder="请输入内容"> <input type="button" value="搜索" class="btn btn-success" onclick=""> </div> </div> </nav> <nav class="navbar navb
{% extends ‘base.html‘ %} {% block denglutitle %}登陆{% endblock %} {% block dengluhead %} <link href="../static/css/mm.css" rel="stylesheet" type="text/css"> <script src="../static/js/mm.js"></script> {% endblock %} {% block denglubody %} <div class="box"> <h2>Login</h2> <div class="input_box"> <input type="text" id="uname" placeholder="Your Name"> </div> <div class="input_box"> <input type="password" id="upass" placeholder="password"> </div> <div id="error_box"><br></div> <div class="input_box"> <button onclick="fnLogin()">Enter</button> </div> </div> {% endblock %}
{% extends ‘base.html‘ %} {% block zhucetitle %}注册{% endblock %} {% block zhucehead %} <link rel="stylesheet" type="text/css" href="../static/css/ww.css"> <script src="../static/js/ww.js"></script> {% endblock %} <div id="container"> <div id="header"><h2 align="center">Registration</h2></div> <div id="content"> <form> <p align="center"> <p align="center"> <input type="text" name="user" id="user" placeholder="Username"> </p> <p align="center"> <p align="center"> <input type="password" name="pass" id="pass" placeholder="Password"> </p> <p align="center"> <p align="center"> <input type="password" name="again" id="again" placeholder="Input again"> </p> <p align="ce
{% extends ‘base.html‘ %} {% block tupiantitle %}图片{% endblock %} {% block tupianhead %} <title>暗夜猎手原画</title> <link rel="stylesheet" href="../static/css/22c.css" ype="text/css"> {% endblock %} {% block tupianbody %} <div> <div class="img"> <a href="http://www.baidu.com/"> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508502727361&di=10c6b8b7e89ba8b2f4aa64a4416aea8d&imgtype=0&src=http%3A%2F%2Flol.wanyx.com%2Fimg.wanyx.com%2Fpublic%2Fupload%2Fhero%2F1436233111.jpg" ></a> <div class="sa"><a href="http://www.baidu.com/">原始维恩</a></div> </div> <div class="img"> <a href="http://www.baidu.com/"> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508480168515&di=4d42c1fad395143bf3bc06f113f8e15e&imgtype=0&src=http%3A%2F%2Fcdn.aixifan.com%2Fdotnet%2Fartemis%2Fu%2Fcms%2Fwww%2F201604%2F23035814tgyvdcay.jpg"></a> <div class="sa"> <a href="http://www.baidu.com/">苍穹之光</a></div> </div> </div> {% endblock %}
ype="radio" name="r1" id="r1" value="stu">student <input type="radio" name="r2" id="r2" value="tea">teacher </p> <div id="error_box"><br></div> <p align="center"> <input type="button" value="registration" onclick="fnRegistration()"> </p> </form> </div> <div id="footer"><p align="right">版权FFR</p></div> </div> {% endblock %}
ar-inverse navbar-fixed-bottom" role="navigation"> <ul class="nav navbar-nav"> <li><img src="http://www.gzcc.cn/2016/images/footer1-logo.png"></li> <li style="color: bisque;">版权所有@FFR</li> </ul> </nav> <script> document.write(Date()) </script> <img src="{{ url_for(‘static‘,filename=‘image/12g.jpg‘) }}" alt="wo" width="300"> {% block denglubody %}{% endblock %} {% block zhucebody %}{% endblock %} {% block tupianbody %}{% endblock %} </body> </html>
以上是关于加载静态文件,父模板的继承和扩展的主要内容,如果未能解决你的问题,请参考以下文章