安装模块
新建一个包,在init.py中,实例蓝图对象
可以创建一个视图文件
安装blueprint模块
新建一个包,编辑init.py
from flask import Blueprint # 蓝图对象 """ 参数1: 名称(项目中必须唯一)不能重名 view.py使用本蓝图 blue1 """ blue1 = Blueprint("blue1",__name__) from . import view
然后新建一个view.py用于新建视图函数,并导入蓝图
from . import blue1 @blue1.route('/hello/') def hello(): return 'hello'
在包外可以创建py文件了:(注意要注册蓝图) app.py
from flask import Flask from flask_script import Manager # Manager为了可以直接在终端中直接运行,然后可以从网页直接查看,方便快捷,上节课笔记中有 from App import blue1 app = Flask(__name__) manager = Manager(app) @app.route('/') def index(): return '首页' # 注册蓝图 app.register_blueprint(blue1) if __name__ == '__main__': manager.run()
可以在终端模式下调试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>
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()) # 存储定义的所有变量locals if __name__ == '__main__': manager.run()
var.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{{ title }}</title> </head> <body> <div> <!-- 这是html的注释 --> {{ dic['name'] }} ---- {{ dic['age'] }} <p>{{ list1[1] }}</p> {# 这是模板的注释 #} </div> </body> </html>
注意:<!---->该注释可在浏览器控制台看到,而模板的注释不会再浏览器后台看到更加保护隐私{# #}
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>
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>
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 %}
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>