django diff flask
- django 配置好了很多东西,相当于一个完整的台式机
- flask 微型框架,就相当于一个主板,其他的米有,需要自己去安装第三方包。
flask
配置flask的虚拟环境
- pip3 install virtualenv
- 进入到你要安装环境的文件夹 virtualenv –no-site-packages (C:\Users\zhangli\AppData\Local\Programs\Python\Python36)flaskenv
- cd flaskenv
- cd Script
- 启动虚拟环境 activate
- 安装包,pip install flask
- 退出虚拟环境: deactivate
运行flask
flask_script 官方的连接方式
- from flask_script import Manager
- manager = Manager(app=app)
- manager.run()
- 启动 python xxx.py runserver -p 8008 -h 0.0.0.0 -d
传参
普通传参
- app.route(‘/hello/<”name>/’)
- def hello(name)
- return ‘name: %s’ % name
指定类型传参
- app.route(‘/hello//’)
- def hello(id)
- return ‘id: %d’ % d
/<’converter:name>/
- string: 默认的str
- int: 整型
- float: 浮点
- path: 返回字符串,可以连带”/” 一起返回,区别str
- uuid: uuid类型,不重复的一串随机数,可用于加密操
路由的改造 –> django 的样子(分–总)
蓝图–管理url的工具,规划url
- pip install flask_blueprint
- 初始化
(1)views 初始化blue
- 初始化
- from flask import Blueprint
- blue = Blueprint(‘first’, name)
(2)init 绑定blue - app.register_blueprint(blueprint=blue)
(3)manage.py 运行app - manager = Manager(app=blue)
- 路由注册
templates 页面的访问
写在app内部
*render_template(‘xxx.html’)
写在app外部
- app
- from flask import send_file
- return send_file(‘../templates/hello.html’)