YWEIEN

导航

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

  1. 用url_for加载静态文件
    1. <script src="{{ url_for('static',filename='js/login.js') }}"></script>
    2. flask 从static文件夹开始寻找
    3. 可用于加载css, js, image文件
  2. 继承和扩展
    1. 把一些公共的代码放在父模板中,避免每个模板写同样的内容。base.html
    2. 子模板继承父模板
      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. 首页、登录页、注册页都按上述步骤改写。

 

<!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/base.css') }}">
    <script src="{{ url_for('static',filename='js/base.js') }}"></script>

    {% block head %}
    {% endblock %}
a
</head>
<body id="myBody">
<nav>
    <img id="myOnOff" onclick="mySwitch()"
         src="{{ url_for('static',filename='image/pic_bulboff.gif') }}" width="30px">
    <a href="{{ url_for('base') }}">首页</a>
    <a href="{{ url_for('regist') }}">注册</a>
    <a href="{{ url_for("login")}}" >登录</a>
</nav>

        {% block main %}
        {% endblock %}

<div class="area"></div>


</div>

</body>
</html>

 

login

{% extends'base.html' %}
{% block title %}
    登录
{% endblock %}
{% block head %}
    <link rel="stylesheet" type="text/css" href="../static/css/login.css">
    <script src="../static/js/login.js"></script>
{% endblock %}

{% block main %}
    <body class="body">
            <div class="box">
        <h2 class="ziti">登录</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="fnLogin()">登录</button>
        </div>
    </div>

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

 

regist

{% extends'base.html' %}

{% block title %}
注册
{% endblock %}

{% block head %}
    <link rel="stylesheet" type="text/css" href="../static/css/login.css">
    <script src="../static/js/login.js"></script>
{% endblock %}

{% block main %}
    <body class="body">
    <div class="box">
        <h2 class="ziti">注册</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 id="error_box"><br></div>
        <div class="input_box">
            <button  onclick="fnRegist()">注册</button>
        </div>
        </div>
    </div>

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

 

from flask import Flask,render_template

app = Flask(__name__)


@app.route('/')
def base():
    return render_template('base.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)

 

 

 

 

posted on 2017-11-08 21:17  YWEIEN  阅读(128)  评论(0编辑  收藏  举报