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

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

时间:2017-11-08 20:54:04      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:lin   需求   com   header   col   自己的   问答   idt   container   


  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. 首页、登录页、注册页都按上述步骤改写。
  4. <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link href="{{ url_for(‘static‘,filename=‘css/index.css‘) }}" rel="stylesheet">
        <script src="{{ url_for(‘static‘,filename=‘js/index.js‘) }}"></script>
        {% block head %}
    {% endblock %}
    </head>
    <body id="myBody">
    <nav class="navbar navbar-inverse" role="navigation">
        <nav class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">首页</a>
            </div>
            <div>
                <ul class="nav navbar-nav">
                    <li><a href="#" onclick="">登陆</a></li>
                    <li><a href="#" onclick="">注册</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            设置 <b class="caret"></b>
                        </a>
                        <ul class="dropdown-menu">
                            <li><a href="#">收藏</a></li>
                            <li><a href="#">分享</a></li>
                            <li><a href="#">搜索</a></li>
                            <li class="divider"></li>
                            <li><a href="#">点赞</a></li>
                        </ul>
                    </li>
                    <li><a href="#">||</a></li>
                </ul>
            </div>
        </nav>
     <li><img id="myOnOff" onclick="mySwitch()" src="http://img.idol001.com/origin/2017/09/29/8980680c9e3bf6f19806f56ca8efca621506666450.jpg"height="30px" width="40px"></li>>
    
        {% endblock %}
    <body>
    {% block main %}
    {% endblock %}
    </body>
    
    <div id="bottom">
          Copyright@ 2017 个人版权,版权所有  
     </div>
    </html>

     

 

{% extendsindex.html %}

{% block head %}
    <link href="{{ url_for(‘static‘,filename=‘css/index.css‘) }}" rel="stylesheet" type="text/css">
    <script src="{{ url_for(‘static‘,filename=‘js/index.js‘) }}"></script>
{% endblock %}

{% block main %}
<div class="box" >
    <div class="jar" ><h2>欢迎</h2></div>

        <div class="input_box">
            账号      <input id="name" type="text" placeholder="请输入用户名"></div><br>
        <div class="input_box">
            密码      <input id="password" type="password" placeholder="请输入密码"></div><br>

        <div id="error_box"><br></div>
         <div class="ja">
            <button onclick="myLogin()" >登陆</button></div>
    </div>
{% endblock %}

 

 

 

{% extendsindex.html %}
 
{% block head %}
    <link href="{{ url_for(‘static‘,filename=‘css/index.css‘) }}" rel="stylesheet" type="text/css">
    <script src="{{ url_for(‘static‘,filename=‘js/index.js‘) }}"></script>
{% endblock %}
 
{% block main %}
<div class="box" >
    <div class="jar" ><h2>欢迎</h2></div>
 
        <div class="input_box">
            账号      <input id="name" type="text" placeholder="请输入用户名">   </div><br>
        <div class="input_box">
            密码      <input id="password" type="password" placeholder="请输入密码"></div><br>
         <div class="input_box">
            再输入     <input id="passwordagain" type="password" placeholder="请再次输入密码"></div><br>
        <div id="error_box"><br></div>
         <div class="ja">
            <button onclick="fnLogin()" >注册</button></div>
    </div>
 
{% endblock %}

 

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

标签:lin   需求   com   header   col   自己的   问答   idt   container   

原文地址:http://www.cnblogs.com/123hyf/p/7799147.html

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