加载静态文件,父模板的继承和扩展

用url_for加载静态文件

1<script src="{{ url_for('static',filename='js/login.js') }}"></script>

2.flask 从static文件夹开始寻找

3.可用于加载css, js, image文件

继承和扩展

把一些公共的代码放在父模板中,避免每个模板写同样的内容。base.html

2.子模板继承父模板

1.  {%.1 extends 'base.html’ %}

3.父模板提前定义好子模板可以实现一些自己需求的位置及名称。block

1.<title>{% block title %}{% endblock %}-MIS问答平台</title>

2.{% block head %}{% endblock %}

3.{% block main %}{% endblock %}

4子模板中写代码实现自己的需求。block

1. {% block title %}登录{% endblock %}

3.首页、登录页、注册页都按上述步骤改写。

flask

from flask import Flask,render_template

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')

@app.route('/login/')
def login():
    return render_template('login.html')

@app.route('/regist/')
def regist():
    return render_template('regist.html')

if __name__== '__main__':
    app.run(debug=True)

index

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block titile%}{% endblock %}MIS15</title>
</head>
<body>
{% block main %}
<a href="http://127.0.0.1:5000">首页</a>
<a href="http://127.0.0.1:5000/login">登录</a>
<a href="{{ url_for('regist') }}">注册</a>

<h1>MIS问答平台</h1>
<h4>2015</h4>
<P></P>

<div  id="container" style="width:400px " >
    <div id="header" style="background-color:#97ceff;"><h2 align="center" style="margin-bottom:0;">登录</h2></div>
    <form>
        <input type="text" name="usertname"placeholder="请输入用户名"><br>
        <input type="password" name="password"placeholder="请输入六位数密码"><br>
        <input type="radio" name="role" value="stu">student
         <input type="radio" name="role" value="stu">teather<br>
        <input type="button" value="确定">
    </form>
    <div id="footer" style="background-color:#ffa7d7;clear:both;text-align:center;">版权 © yan</div>

<form>
    <select>
        <option>问答</option>
        <option>收藏</option>
    </select>
</form>

    <ol>
        <li>python</li>
        <li>html</li>
    </ol>

</div>
<hr>
<p>友情链接</p>
<a href="http://www.gzcc.cn/">
    <img src="http://www.gzcc.cn/2016/images/banner.png" width="400" height="50" alt="gzcc.cn"  />
    <br>广州商学院
</a>

{% endblock %}
</body>
</html>

login

{% extends 'index.html' %}
{% block titile %}
登陆
{% endblock %}
{% block head %}
<head>
<link href="../static/css/text.css" rel="stylesheet" type="text/css">
<script src="../static/js/text.js"></script>

</head>
<body>
<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>
</body>
</html>
{% endblock %}

regist

{% extends 'index.html' %}
{% block titile %}
注册
{% endblock %}
{% block head %}
<head>
<link href="../static/css/text.css" rel="stylesheet" type="text/css">
<script src="../static/js/text.js"></script>

</head>
<body>
<div class="box">
<h2>欢迎进入</h2>
<h3>VIP Register</h3>

<div class="input_box">
            请输入邮箱   <input id="email" type="text" placeholder="需要通过邮件激活账户">  </div><br>
        <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>
</body>
</html>
{% endblock %}

 

posted on 2017-11-08 13:26  081肖妍  阅读(115)  评论(0编辑  收藏  举报

导航