Flask 2 --- 蓝图
一. 蓝图的用法
作用:将代码模块化,便于管理。
使用步骤:
- 
安装模块 
- 
新建一个包,在init.py中,实例蓝图对象 
- 
可以创建一个视图文件 
举例:
安装blueprint模块
新建一个包,编辑init.py
from flask import Blueprint
然后新建一个view.py用于新建视图函数,并导入蓝图
from . import blue1
@blue1.route('/hello/')
def hello():
    return 'hello'
在包外可以创建py文件了:(注意要注册蓝图) app.py
from flask import Flask
from flask_script import Manager        
 
可以在终端模式下调试app.py
python app.py runserver -d -p 1111
二.模板
 
from flask import Flask, render_template
app = Flask(__name__)
manager = Manager(app)
@app.route('/')
def hello_world():  
    return render_template('index.html',title='首页',content='这是一个模板!')
if __name__ == '__main__':
    manager.run()
index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
<h1>
    {{ content }}
</h1>
</body>
</html>
 
三.扩展
1.注释和locals的用法
from flask import Flask, render_template
app = Flask(__name__)
manager = Manager(app)
@app.route('/var/')
def show_var():
    list1 = [1,2,3,5]
    dic = {'name':'王钢蛋','age':19}
    title = 'show'
    return render_template('var.html',**locals())       
 
var.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
</head>
<body>
<div>
    
注意:
2.算术运算符
from flask import Flask, render_template
app = Flask(__name__)
manager = Manager(app)
@app.route('/express/')
def show_express():
    a = 10
    b = 10
    name = 'tom'
    c = [1,2,3,4,5,6]
    return render_template('express.html',**locals())
if __name__ == '__main__':
    manager.run()
 
express.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #myadd{width: 100px;height: 100px;background: aquamarine}
        #myif{width: 100px;height: 100px;background: green}
    </style>
</head>
<body>
<div id="myadd">
    <p>{{ a }}+{{ b }}={{ a+b }}</p>
    <p>{{ name + " hello" }}</p>
</div>
<div id="myif">
    {% if a>0 %}
        <p>{{ a }}</p>
    {% endif %}
</div>
</body>
</html>
 
3.if语句
from flask import Flask, render_template
app = Flask(__name__)
manager = Manager(app)
@app.route('/logic/')
def show_logic():
    a = [1,23,4,234]
    b = 20
    return render_template('logic.html',**locals())
if __name__ == '__main__':
    manager.run()
 
logic.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #box1{width: 100px;background: bisque;}
        #box2{width: 100px;background: chocolate;}
        #box3{width: 100px;background: red;}
    </style>
</head>
<body>
<div id="box1">
    {% for i in a %}
        <p>{{ i }}</p>
    {% else %}
        <p>nothing</p>
    {% endfor %}
</div>
<div id="box2">
    {% if b>=90 %}
        <p>分数为{{ b }}的是优秀生!</p>
    {% endif %}
</div>
<div id="box3">
    {% if b>=60 and b<=80 %}
        <p>分数为{{ b }}的是普通学生!</p>
    {% elif b<60 %}
        <p>分数为{{ b }}的是差生!</p>
    {% endif %}
</div>
</body>
</html>
 
4.继承
from flask import Flask, render_template
app = Flask(__name__)
manager = Manager(app)
@app.route('/base/')
def show_base():
    return render_template('childpage.html',**locals())
if __name__ == '__main__':
    manager.run()
 
base.html(父html):
<!DOCTYPE html>
<html lang="en">
<head>
    {% block head %}
        <meta charset="UTF-8">
        {% block css %}
            <link rel="stylesheet" href="/static/css/base.css">
        {% endblock css %}
        {% block title %}
        <title>Title</title>
        {% endblock title %}
    {% endblock head %}
</head>
<body>
{% block navi %}
    <div class="class1">this is div1</div>
{% endblock navi %}
{% block content %}
    <p>this is father's content</p>
{% endblock content %}
</body>
</html>
 
childpage.html(子html)
{% extends 'base.html' %}
{% block content %}
    <p>这是保留父模板{{ super() }}</p>
    <p>继承父模板(这个内容是改写父模板)</p>
{% endblock content %}
 
5.静态资源的访问
from flask import Flask, render_template
from flask_script import Manager
app = Flask(__name__)
manager = Manager(app)
@app.route('/img/')
def show_img():
    return render_template('static.html')
if __name__ == '__main__':
    manager.run()
 
static.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{#<img src="/static/images/01.jpg" alt="">#}
<img src="{{ url_for("static",filename="images/01.jpg",_external=True) }}" alt="">
</body>
</html>