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


  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. 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('/register/')
    def register():
    return render_template('register.html')

    if __name__ == '__main__':
    app.run(debug=True)

     

 导航页的代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--<title>父模板</title>
<title>{% block title%}
{% endblock %} wk</title>
<!--<script src="../static/js/base.js" type="text/javascript"></script>-->

<script src="{{ url_for('static',filename='js/base.js') }}" type="text/javascript"></script>
<link rel="stylesheet" href="../static/css/base.css">
{% block head %}
{% endblock %}
</head>
<body id="myBody">
<nav class="body">
<div style="background-color: antiquewhite">
<img id="myOn_Off" onclick="mySwitch()" src="http://www.runoob.com/images/pic_bulbon.gif" width="25" height="25">
<a href="{{ url_for('base') }}">首页</a>
<a href="">下载APP</a>
搜索框<input id="search" type="text" name="search" placeholder="请输入搜索内容:">
<button id="play" type="submit">查找</button>
<ul class="nav navbar-nav">
<a class="login" href="{{ url_for('login') }}">登录</a>
<a class="register" href="{{ url_for('register') }}">注册</a>

</div>
<br>
</nav>
<div class="logo">
</div>


<div class="img">
<a href="https://baike.so.com/doc/2156196-2281474.html">
<img src="https://p1.ssl.qhmsg.com/t01545bf9b68092711f.jpg"></a>
<div class="describe"><a href="">迪丽热巴</a></div>
</div>
<div class="img">
<a href="https://baike.so.com/doc/2156196-2281474.html">
<img src="https://p1.ssl.qhmsg.com/t018e004ed8688e16cb.jpg"></a>
<div class="describe"><a href="">迪丽热巴</a></div>
</div>
<div class="img">
<a href="https://baike.so.com/doc/2156196-2281474.html">
<img src="https://p1.ssl.qhmsg.com/t018115ba19a408a65a.jpg"></a>
<div class="describe"><a href="">迪丽热巴</a></div>
</div>

</div>
{% block main %}
{% endblock %}
<footer>
<div class="footer_box">
Copyright@2013-14-520 个人版权,版权所有 telephone:520520 mobile phone:666
</div>
</footer>
</div>

</body>
</html>

 

 

 

 

登录:

{% extends 'base.html' %}

{% block denglutitle %}登录{% endblock %}

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

{% block denglubody %}
    <div class="box">
        <h2>登录</h2>
        <form action="{{ url_for('gg') }}" method="post">
        <div class="input_box">
            username:<input type="text" id="uname" placeholder="请输入用户名" name="username">
        </div>
        <div class="input_box">
            password:<input type="password" id="upass" placeholder="请输入密码" name="password">
        </div>
        <div id="error_box"><br></div>
        <div class="input_box">
            <button onclick="fnLogin()">Enter</button>
        </div>
        </form>
    </div>
{% endblock %}

注册:

{% extends 'base.html' %}
{% block zhucetitle %}注册{% endblock %}
{% block zhucehead %}
    <title>Registration</title>
    <link rel="stylesheet" type="text/css" href="../static/css/zhuce.css">
    <script src="../static/js/zhuce.js"></script>
{% endblock %}
{% block zhucebody %}
     <div class="box">
          <h2>注册</h2>

     <form action="{{ url_for('login') }}" method="post">
          <div class="input_box">
                <input type="text" id="user" placeholder="请输入用户名" name="username">
          </div>


          <div class="input_box">
                <input type="password" id="upass" placeholder="请输入密码"name="password">
          </div>

          <div class="input_box">
                <input type="password" id="upass1" placeholder="请再次确认密码">
          </div>

          <div id="error_box"><br>
          </div>

          <div class="input_box">
            <button onclick="fnRegistration()">Enter</button>
          </div>
         </form>
     </div>
{% endblock %}


 

 

 

 

 

 

posted on 2017-11-08 20:17  欢喜小卢  阅读(204)  评论(0编辑  收藏  举报

导航