zoukankan      html  css  js  c++  java
  • 加载静态文件,父模板的继承和扩展

    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. 首页、登录页、注册页都按上述步骤改写。

    py:

    from flask import Flask,render_template
    
    app = Flask(__name__)
    
    
    @app.route('/')
    def daohang():
        return render_template('daohang.html')
    @app.route('/login/')
    def login():
        return render_template('1031.html')
    @app.route('/regist/')
    def regist():
        return render_template('regist.html')
    if __name__ == '__main__':
        app.run(debug=True)

    首页:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}{% endblock %}首页</title>
        <link type="text/css" rel="stylesheet" href="{{ url_for('static',filename='/css/index.css') }}">
        <script src="{{ url_for('static',filename='js/moonlight.js') }}"></script>
        {% block head %}{% endblock %}
    </head>
    <body id="myBody">
    <nav>
        <img src="http://cdn2.jianshu.io/assets/web/logo-58fd04f6f0de908401aa561cda6a0688.png" width="40px">
        <a href="{{ url_for('daohang') }}">首页</a>
        <a href="http://www.jianshu.com/">下载app</a>
        <input type="text" name="search">
        <button type="submit">搜索</button>
        <a href="{{ url_for('login') }}">登陆</a>
        <a href="{{ url_for('regist') }}">注册</a>
        <img id="myOnOff" src="http://www.runoob.com/images/pic_bulbon.gif" onclick="mySwitch()" width="20px">
    </nav>
    {% block main %}{% endblock %}
    </body>
    </html>

    登录:

    {% extends'daohang.html' %}
    {% block title %}登录{% endblock %}
    {% block head %}
        <link rel="stylesheet" href="{{ url_for('static',filename='css/1031.css') }}" type="text/css">
        <script src="{{ url_for('static',filename='/js/js.js') }}"></script>'
    
    {% endblock %}
    {% block main %}
        <div class="box" >
        <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="fnLogin()">登录</button>
            <button type="button" onclick=window.alert("账号密码为空,请输入!")>未输入账号密码</button>
        </div>
    </div>
    {% endblock %}

     注册:

    {% extends'daohang.html' %}
    {% block title %}注册{% endblock %}
    {% block head %}
        <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/1031.css') }}">
        <script src="{{ url_for('static',filename='js/zhuce1.js') }}"></script>
    {% endblock %}
    
    {% block main %}
    <div class="box" >
        <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="upass1" type="password" placeholder="请再次输入密码">
        </div>
        <div id="error_box"><br></div>
        <div class="input_box">
            <button onclick="fnLogin()">注册</button>
        </div>
    </div>
    {% endblock %}

  • 相关阅读:
    linux中如何修改文件夹的用户权限 chown命令
    httpserver
    协程
    进程和线程的区别和联系
    python线程的GIL问题(全局解释器锁)
    线程同步互斥的方法
    threading模块创建线程
    信号量(信号灯)
    信号通道
    内存共享
  • 原文地址:https://www.cnblogs.com/chenhuafei/p/7781054.html
Copyright © 2011-2022 走看看