当前位置:网站首页>Flask 轻量web框架
Flask 轻量web框架
2022-07-04 15:43:00 【Wu_Candy】
【前置条件:你已经在windows下搭建好了Pycharm集成开发工具+python3.6的版本及相关的第三方包】
Step1:直接上框架全图
Step2:框架的结构说明
项目结构
- manage.py 用来控制程序的 python manage.py runserver #启动服务命令 python manage.py shell #进入shell脚本模式命令 - app/__init__ 初始化文件 初始化整个Flask对象,以及Flask所用的各种插件,并返回一个app应用 - app/settings 项目配置文件 配置整个项目运行环境 - app/ext 项目的扩展库 第三方扩展库打包处理 - app/views 视图函数 处理业务逻辑 协调模块和模型之间的关系 - app/models 模型 定义模型结构 获得数据库中的表的关系映射 db.create_all() 创建数据库 db.drop_all() 删除数据库 - doc 框架说明文档 - static 用于存放静态资源的目录 - templates 用于存放模板资源的目录 - migrations 迁移目录
数据迁移
- 将模型映射到数据库 - 使用flask-migrate库 - 安装与使用 pip install flask-migrate 初始化,需要使用app和db进行初始化,migrate=Migrate(app,db) 配置flask-script 的命令 manager.add_command('db',MigrateCommand)
- 指令使用 python manage.py db init init 初始化指令,仅可调用一次 python manage.py db migrate migrate指令用于生成迁移文件 内部迁移文件使用了链表来关联关系 --message MSG 对迁移添加日志说明 python manage.py db upgrade upgrade指令用于执行迁移文件 数据库内容升级 python manage.py db downgrade downgrade指令用于执行迁移文件 数据库内容降级,相当于后悔药
- help 帮助文档
Step3:大概的调用逻辑如下说明
<1> App/views.py 文件引入了models.py【模型包中的模型对象】andext.py【拓展包中的db 对象】
<2> App/__init__.py 文件引入了views.py【视图包里面的蓝图】,ext.py【引入拓展包并在拓展包中做初始化工作】,settings.py【引入设置包并做数据库连接和环境配置及资源目录的设置工作】。最后初始化完成app 应用后,返回一个app应用。
<3> manage.py 是入口文件,拿到上一步骤中返回的app应用,最后就是启动服务。
Step4:服务启动后访问首页效果是
Step5:针对上图看看路由与页面的源码
其实是去渲染Templates/home.html页面
App/views.py:
@blue.route('/index/',methods=['GET','POST']) def index(): return render_template('home.html')
页面主要是登录与退出的两个链接
Templates/home.html 页面源码为:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>welcome back:{{ username }}</h2> <a href="{{ url_for('second_blue.login') }}">login</a> <a href="{{ url_for('second_blue.logout') }}">logout</a> </body> <html>
边栏推荐
- 新享科技发布小程序UniPro小优 满足客户移动办公场景
- 缓存穿透、缓存击穿、缓存雪崩分别是什么
- 金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
- leetcode刷题目录总结
- 一文掌握数仓中auto analyze的使用
- World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
- Great Wall Securities security does not open a securities account
- go-micro教程 — 第二章 go-micro v3 使用Gin、Etcd
- 中信证券网上开户安全吗 开户收费吗
- NFT liquidity market security issues occur frequently - Analysis of the black incident of NFT trading platform quixotic
猜你喜欢

VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题

Kunming Third Ring Road Closure project will pass through these places. Is there one near your home?

建筑建材行业经销商协同系统解决方案:赋能企业构建核心竞争力

【云原生】服务网格是什么“格”?

【HCIA持续更新】广域网技术

The winning rate against people is 84%, and deepmind AI has reached the level of human experts in army chess for the first time

一加10 Pro和iPhone 13怎么选?

解读数据安全治理能力评估框架2.0,第四批DSG评估征集中

Solution of dealer collaboration system in building materials industry: empowering enterprises to build core competitiveness

解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
随机推荐
R language plot visualization: plot visualizes overlapping histograms and uses geom at the top edge of the histogram_ The rug function adds marginal rug plots
Difference between redis' memory obsolescence strategy and expiration deletion strategy
R语言plotly可视化:plotly可视化互相重叠的直方图(historgram)、并在直方图的顶部边缘使用geom_rug函数添加边缘轴须图Marginal rug plots
VB cannot access database stocks
What is low code development?
整理混乱的头文件,我用include what you use
tp配置多数据库
OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
Cocoscreator event dispatch use
[glide] cache implementation - memory and disk cache
PingCode 性能测试之负载测试实践
【Go ~ 0到1 】 第六天 文件的读写与创建
[template] [Luogu p4630] duathlon Triathlon (round square tree)
Capvision Rongying's prospectus in Hong Kong was "invalid": it was strictly questioned by the CSRC and required supplementary disclosure
Go development: how to use go singleton mode to ensure the security of high concurrency of streaming media?
利用win10计划任务程序定时自动运行jar包
开发者,MySQL专栏完更,助你轻松从安装到入门进阶
The 18th IET AC / DC transmission International Conference (acdc2022) was successfully held online
kaili不能输入中文怎么办???
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection