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

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

时间:2017-11-04 20:37:44      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:login   自己   copy   www.   input   log   账号   flask   sele   

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

1.

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

 <li id="myBody"  style="float: right"> <img id="myOnOff" onclick="mySwitch()" src="{{url_for( ‘static‘,filename=‘img/on.jpg‘)}}" width="40px"></li>
        <li style="float:right"><a href="{{ url_for(‘login‘) }}" target="_blank">登录</a></li>
        <li style="float:right"><a href="{{ url_for(‘register‘) }}" target="_blank">注册</a></li>

2.base.html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>{% block title %}
       {% endblock  %}
        baseTest</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  %}
</head>

<div class="navbar">
    <nav class="navbar-top">
        <li class="active"><a href="{{ url_for(‘index‘) }}" target="_blank">首页</a></li>
        <li><a href="#">歌单</a></li>
        <li><a href="#">电台</a></li>
        <li><a href="#">音乐人</a></li>
        <li><input type="text" id="select" placeholder="音乐搜索,找人"></li>
        <li id="submit"><a href="#">搜索</a></li>
        <li id="myBody"  style="float: right"> <img id="myOnOff" onclick="mySwitch()" src="{{url_for( ‘static‘,filename=‘img/on.jpg‘)}}" width="40px"></li>
        <li style="float:right"><a href="{{ url_for(‘login‘) }}" target="_blank">登录</a></li>
        <li style="float:right"><a href="{{ url_for(‘register‘) }}" target="_blank">注册</a></li>


    </nav>
</div>
<body id="myBody">
<footer>
    <div class="row">
            <div class="col-sm-12">
      <div style="text-align: center;">

        <hr>

                <p>Copyright ? 2013-2017<a target="_blank" href="//www.runoob.com/">菜鸟教程</a></p>
         </div>
            </div>
        </div>
</footer>
{% block main %}

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

 主页代码:

{% extends ‘base.html‘ %}
{% block title %}
    index
{% endblock  %}

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

{% block main %}
<div class="recommand">
    <div class="col-sm-12">
                <div class="index-normalNews-header-focus">
                    <h4 class="normalNewstitle">用户导航</h4>
                 </div>
        <hr>
            </div>
    <div class="img">
        <a href="#"><img src="{{ url_for(‘static‘,filename=‘img/test1.jpg‘) }}"></a>
        <div class="dese"><a href="#">Test1</a></div>
    </div>
    <div class="img">
        <a href="#"><img src="{{ url_for(‘static‘,filename=‘img/test2.jpg‘) }}"></a>
        <div class="dese"><a href="#">Test2</a></div>
    </div>
    <div class="img">
        <a href="#"><img src="{{ url_for(‘static‘,filename=‘img/test3.jpg‘) }}"></a>
        <div class="dese"><a href="#">Test3</a> </div>
    </div>
    <div class="img">
        <a href="#"><img src="{{ url_for(‘static‘,filename=‘img/test4.jpg‘) }}"></a>
        <div class="dese"><a href="#">Test4</a> </div>
    </div>
</div>
<br>
{% endblock  %}

登录页代码:

{% extends ‘base.html‘ %}
{% block title %}
    login
{% endblock  %}

{% block head %}

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

{% endblock  %}

{% block main %}
<div class="box">
    <div id="title">LoginText</div>
    <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="fnLogin()">登录</button>
        <a href="{{ url_for(‘register‘) }}">注册/Register</a>
    </div>
</div>

{% endblock  %}

注册页代码:

{% extends ‘base.html‘ %}
{% block title %}
    Register
{% endblock  %}
{% block head %}

    <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘css/10.31.css‘) }}">
    <script src="{{ url_for(‘static‘,filename=‘js/register.js‘) }}"></script>

{% endblock  %}

{% block main %}
<div class="box">
    <div id="title">RegisterText</div>
    <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 class="input-box">
        验证:<input id="upass1" type="password" placeholder="请再次输入密码">
    </div>
    <div id="error-box"><br></div>
    <div class="input-box">
        <button onclick="fnRegister()">注册/Register</button>
        <a href="{{ url_for(‘login‘) }}">已注册/Login</a>
    </div>
</div>

{% endblock  %}

效果图如下:

技术分享

主页

技术分享

登录页

技术分享

注册页

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

标签:login   自己   copy   www.   input   log   账号   flask   sele   

原文地址:http://www.cnblogs.com/liminghui3/p/7780459.html

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