码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-11-03 19:01:44      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:function   ima   har   log   mis   targe   run   nav   sub   

  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/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 %}

    技术分享技术分享技术分享

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

标签:function   ima   har   log   mis   targe   run   nav   sub   

原文地址:http://www.cnblogs.com/JUNJUNER/p/7779353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!