flask 环境environment,运行Manager,路由管理Blueprint

django diff flask

  1. django 配置好了很多东西,相当于一个完整的台式机
  2. flask 微型框架,就相当于一个主板,其他的米有,需要自己去安装第三方包。

flask

配置flask的虚拟环境

  1. pip3 install virtualenv
  2. 进入到你要安装环境的文件夹 virtualenv –no-site-packages (C:\Users\zhangli\AppData\Local\Programs\Python\Python36)flaskenv
    • cd flaskenv
    • cd Script
  3. 启动虚拟环境 activate
  4. 安装包,pip install flask
  5. 退出虚拟环境: deactivate

运行flask

flask_script 官方的连接方式

  1. from flask_script import Manager
  2. manager = Manager(app=app)
  3. manager.run()
  4. 启动 python xxx.py runserver -p 8008 -h 0.0.0.0 -d

传参

普通传参

  1. app.route(‘/hello/<”name>/’)
  2. def hello(name)
  3. return ‘name: %s’ % name

指定类型传参

  1. app.route(‘/hello//’)
  2. def hello(id)
  3. return ‘id: %d’ % d

/<’converter:name>/

  1. string: 默认的str
  2. int: 整型
  3. float: 浮点
  4. path: 返回字符串,可以连带”/” 一起返回,区别str
  5. uuid: uuid类型,不重复的一串随机数,可用于加密操

路由的改造 –> django 的样子(分–总)

蓝图–管理url的工具,规划url

  • pip install flask_blueprint
    1. 初始化
      (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)
    1. 路由注册

templates 页面的访问

写在app内部

*render_template(‘xxx.html’)

写在app外部

  1. app
    • from flask import send_file
    • return send_file(‘../templates/hello.html’)

猜你喜欢

转载自blog.csdn.net/zhangli709/article/details/80311715