蓝图
- 创建蓝图对象
Blueprint(self,name,import_name)- name是蓝图名称,不要重名
- import_name 模块名
- url_prefix 路由前缀: 以/开头,结尾不要有/
from flask import Blueprint
bp = Blueprint('bp',__name__,url_prefix='/bp')
- 注册
app.register_blueprint(bp)
app
from flask import Flask
from flask_script import Manager
# 导入蓝图对象
from views import bp
app = Flask(__name__)
manager = Manager(app)
# 注册蓝图对象
app.register_blueprint(bp)
if __name__ == '__main__':
manager.run()
##views
from flask import Blueprint, render_template
bp = Blueprint('bp',__name__,url_prefix='/bp')
# 视图函数不能重名
@bp.route('/call/')
def call_me():
# 如果有蓝图,url_for("蓝图名.视图函数名")
return redirect(url_for("bp.render_vars"))
@bp.route("/test/")
def render_test():
return render_template('test.html',num=90)
@bp.route("/express/")
def render_express():
return render_template('表达式.html',num=10)
@bp.route("/control/")
def render_conrol():
return render_template('流程控制.html',num=30,list1=[1,2,3,4,5])
@bp.route('/include/')
def render_include():
return render_template('包含.html')
@bp.route("/macro/")
def render_maro():
return render_template('宏.html')
@bp.route('/child/')
def render_child():
return render_template('comm/child.html',title="继承")
@bp.route('/boot/')
def render_bootstrap():
return render_template('继承2.html')
test.html
if num is iterable
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<p>
{% if num is iterable %}
<h2>可迭代</h2>
{% endif %}
</p>
</body>
</html>
表达式.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表达式</title>
</head>
<body>
<p>
{
{ 3 + 6 }}---{
{ 10 % 5 }}
</p>
<p>
{# 注释:关系运算符 #}
{
{ 5 > 3 }}
</p>
<p>
{# 逻辑运算符 #}
{
{ num >0 and num < 20 }}
</p>
<p>
{# {% in is if-else %}#}
<h2>in成员运算符</h2>
{
{ num in [1,2,3, 10] }}
{
{ 100 if num > 0 else 50}}
</