加载静态文件,父模板的继承和扩展
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> <head> <meta charset="utf-8"> <title>{%block title %}{% endblock %}导航</title> <link rel="stylesheet" type="text/css" href="{{url_for(\'static\',filename=\'css/daohang.css\')}}"> </head> <body bgcolor="#00ffff"> <ul> <li><a class="active" >首页</a></li> <li><a >新闻</a></li> <li><a >图库</a></li> <li><a >关于</a></li> <input class="eat" type="text" name="sousuo" value="请输入内容"> <input class="eat"type="button" name="submit" value="搜索"> <li><a href="http://127.0.0.1:5000/login/">登录</a></li> <li><a href="http://127.0.0.1:5000/zhu/">注册</a></li> </ul> {% block main %}{% endblock %} </body> </html>
ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover:not(.active) { background-color: #111; } .active { background-color: #4CAF50; } .eat{ margin-top:0.4cm; } .tea{ border:2px solid #a1a1a1; padding:10px 500px; background:hotpink; width:300px; border-radius:25px; } #rcorners6 { background: #8AC007; padding: 5px; width: 1300px; height: 50px; }
{% extends\'daohang.html\' %} {% block title %} <h1>登录</h1> {% endblock %} {% block main %} <head> <link rel="stylesheet" type="text/css" href="{{ url_for(\'static\',filename=\'css/denglu.css\') }}"> <script src="../static/js/muban.js"></script> </head> <body> <div class="one"> <h2>登录</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 class="input_box"> <button onclick="myLogin()">登录</button> </div> </div> </div></div> </body> {% endblock %}
.one{ border-style:dotted solid; border-color:royalblue; border-width:5px; margin: 0 auto; text-align: center; background-color:palevioletred; width: 300px; } h2{ color:black; text-align:center; } .input_box{ width: 300px; height: 30px; border-bottom-width: 2px; line-height: 30px; font-weight: bold; } .input_box{ font-size: 8px; font-weight: bold; border-color: blanchedalmond; } .error_box{ width: 80px; height: 30px; border-bottom-width: 2px; line-height: 30px; font-weight: bold; background-color:palevioletred; }
{% extends\'daohang.html\' %} {% block title %} <h1>注册页面</h1> {% endblock %} {% block main %} <head> <link rel="stylesheet" type="text/css" href="{{url_for(\'static\',filename=\'css/zhuce.css\')}}"> <script src="{{url_for(\'static\',filename=\'js/muban.js\')}}"></script> </head> <body> <div class="two"> <h2>用户注册</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="upass" type="password" placeholder="请再次确认密码"> </div> <div id="error_box"><br></div> <div class="input_box"> <button onclick="myLogin()">注册</button> </div> </div> </div> </div> </body> {% endblock %}
.two{ border-style:dotted solid; border-color:royalblue; border-width:5px; margin: 0 auto; text-align: center; background-color:palevioletred; width: 300px; } h2{ color:black; text-align:center; } .input_box{ width: 300px; height: 30px; border-bottom-width: 2px; line-height: 30px; font-weight: bold; } .input_box{ font-size: 8px; font-weight: bold; border-color: blanchedalmond; } .error_box{ width: 80px; height: 30px; border-bottom-width: 2px; line-height: 30px; font-weight: bold; background-color:palevioletred; }
function fnlogin() { var oUname = document.getElementById("uname"); var oUpass = document.getElementById("upass"); var oPas=document.getElementById("upas") var oError = document.getElementById("error_box"); oError.innerHTML = "<br>" if (oUname.value.length < 6 || oUname.value.length > 12) { oError.innerHTML = "name:6-20"; return; } else if(oUname.charCodeAt(0)>=48&&oUname.charCodeAt(0)<=57){ oError.innerHTML="first number."; return; }else for(var i=0;i<oUname.value.length;i++){ if((oUname.value.charCodeAt(i)<48||oUname.value.charCodeAt(i)>57)&&(oUname.value.charCodeAt(i)<97||oUname.value.charCodeAt(i)>122)){ oError.innerHTML="only letter or number." } } if(oPas.value!=oPass.value){ oError.innerHTML=\'密码输入不正确\' } if (oUpass.value.length < 6 || oUpass.value.length >20) { oError.innerHTML = "password:6-20"; return; } window.alert("登录成功!") }
以上是关于加载静态文件,父模板的继承和扩展的主要内容,如果未能解决你的问题,请参考以下文章