加载静态文件,父模板的继承和扩展
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 %}
- 首页、登录页、注册页都按上述步骤改写。
<!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/99.css\')}}"> <base href="" target="_blank"> {% block head %} {% endblock %} <script> function myswitch() { var oBody = document.getElementById("myBody"); var oOnoff = document.getElementById("myOnOff"); if(oOnoff.src.match("bulbon")){ oOnoff.src="http://www.runoob.com/images/pic_bulboff.gif"; oBody.style.background="black"; oBody.style.color ="white"; }else{ oOnoff.src="http://www.runoob.com/images/pic_bulbon.gif"; oBody.style.background="white"; oBody.style.color ="black"; } } </script> </head> <body> <nav> <img src="http://img.mp.itc.cn/upload/20170720/25678b6966bf4a79ae9d7b85df4f3531_th.jpg" width="40" height="40" alt="img.mp.itc.cn"> <input type="text"> <button type="submit">搜索</button> <a href="{{ url_for(\'index\') }}">首页</a> <a href="http://127.0.0.1:5000/login">登录</a> <a href="{{ url_for(\'regist\') }}">注册</a> <body id="myBody"> <img id="myOnOff" onclick="myswitch()" src="http://www.runoob.com/images/pic_bulbon.gif" width="20px"> <script>document.write(Date())</script> </body> </nav> {% block main %} {% endblock %} </body> </html>
{% extends\'index.html\' %} {% block title %}登录 {% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for(\'static\',filename=\'css/886.css\') }}"> <script src="{{ url_for(\'static\',filename=\'js/tt.js\')}}"></script> {% endblock %} {% block main %} <div class="box"> <h2 class="dd">登录</h2> <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> <button class="jj" onclick="fnLogin()">登录</button> </div> </div> {% endblock %}
{% extends\'index.html\' %} {% block title %}注册 {% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for(\'static\',filename=\'css/886.css\') }}"> <script src="{{ url_for(\'static\',filename=\'js/00.js\')}}"></script> {% endblock %} {% block main %} <div class="box"> <h2 class="dd">注册</h2> <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> <button class="jj" onclick="tnlogin()">注册</button> </div> </div> {% endblock %}
以上是关于加载静态文件,父模板的继承和扩展的主要内容,如果未能解决你的问题,请参考以下文章