准备工作
开始flask项目的步骤:1.?安装python环境 2.?检验python和pip是否安装好 3.?新建项目目录 4.?安装 Virtual env 5.?使用pipenv,这样可省略第4步 6.?启动pipenv 7.?安装各种包,比如flask
建议使用官方推荐的pipenv工具来创建虚拟环境,该虚拟环境和项目绑定。
进行安装:在Windows命令行下(在 Linux 下也适用),
cd?Desktop mkdir?fisher????????????????#创建项目目录 cd?fisher pip?install?-i?https://pypi.tuna.tsinghua.edu.cn/simple?pipenv??????????????#安装pipenv
Windows下
pipenv?install??????????????#创建虚拟环境 pipenv?shell????????????????#进入虚拟环境 pipenv?install?-i?https://pypi.mirrors.ustc.edu.cn/simple??flask????????????????#安装flask flask???????????????#验证,未报错则安装成功
Linux 下
#?echo?'PATH=$PATH:/usr/python/bin'?>>?/etc/profile?&&?source?/etc/profile#?pipenv?install#?pipenv?shell#?pipenv?install?-i?https://pypi.mirrors.ustc.edu.cn/simple??flask#?flask
pip或pipenv安装慢时可换成国内源:
阿里云:http://mirrors.aliyun测试数据/pypi/simple 豆瓣:http://pypi.douban测试数据/simple 清华大学:https://pypi.tuna.tsinghua.edu.cn/simple 中国科学技术大学:https://pypi.mirrors.ustc.edu.cn/simple
Linux 下永久修改
#?mkdir?~/.pip#?vim?~/.pip/pip.conf?[global]index-url=https://pypi.tuna.tsinghua.edu.cn/simple/[install]trusted-host=pypi.tuna.tsinghua.edu.cnpipenv常用命令:
exit????????????????#退出虚拟环境 pipenv??????????????#进入虚拟环境 pipenv?install??????????????#安装包 pipenv?uninstall????????????????#卸载包 pipenv?graph????????????????#查看已安装包的依赖关系开发工具选择:
1.?Pycharm(VS?Code) 2.?Xampp(MySQL) 3.?Navicat(数据库可视化管理工具)
flask最小原型与唯一URL原则
使用PyCharm,将之前桌面创建的fisher目录作为项目路径,打开 文件 → 设置 → 项目: fisher → Project Interpreter ,可在命令行下通过 pipenv --venv 查询,设置与之对应的Project Interpreter。
创建flask入口文件 fisher.py :flask最小原型
from?flask?import?Flask app?=?Flask(__name__)@app.route('/hello/')????????????????#路由注册def?hello(): ????return?'Hello,?World!'app.run()
命令行运行
python?fisher.py
打开浏览器,访问 127.0.0.1:5000/hello
此时发生了重定向,从 127.0.0.1:5000/hello 重定向到 127.0.0.1:5000/hello/ ,这保证了唯一URL原则。
路由的另一种注册方式
开启调试模式:为了避免在修改代码后频繁重启服务器,所以需要开启调试模式,调试模式不要在生产模式下开启。
from?flask?import?Flask app?=?Flask(__name__)@app.route('/hello/')def?hello(): ????return?'Hello,?World!'app.run(debug=True)?????????????#开启调试模式
命令行运行
python?fisher.py另一种方式注册路由:
from?flask?import?Flask app?=?Flask(__name__)def?hello(): ????return?'Hello,?World!'app.add_url_rule('/hello/',?view_func=hello)app.run(debug=True)
一般情况建议使用装饰器方式注册路由,更为简洁优雅;在基于类的视图(即插视图)时使用 app.add_url_rule() 方式注册路由。
app.run相关参数与flask配置文件
接受所有地址访问:from?flask?import?Flask app?=?Flask(__name__)@app.route('/hello/')def?hello(): ????return?'Hello,?World!'app.run(host='0.0.0.0',?debug=True)
此时,可以通过外网地址进行访问,下面通过本机ip进行访问
更改端口:
from?flask?import?Flask app?=?Flask(__name__)@app.route('/hello/')def?hello(): ????return?'Hello,?World!'app.run(host='0.0.0.0',?port=81?debug=True)
此时,就只能通过81端口进行访问了
创建flask配置文件 config.py :
DEBUG?=?True????????????????#常量应该大写
配置文件中的所有参数必须全部大写。
from?flask?import?Flask app?=?Flask(__name__)app.config.from_object('config')????????????????#导入配置文件@app.route('/hello/')def?hello(): ????return?'Hello,?World!'app.run(host='0.0.0.0',?port=81,?debug=app.config['DEBUG'])
if __name__ 的作用:
from?flask?import?Flask app?=?Flask(__name__)app.config.from_object('config')@app.route('/hello/')def?hello(): ????return?'Hello,?World!'if?__name__?==?'__main__': ????app.run(host='0.0.0.0',?port=81,?debug=app.config['DEBUG'])
if __name__ == '__main__' 保证if下面的执行语句只在入口文件中执行。
而且在生产环境下,web服务器通常是由nginx+uwsgi组成的, fisher.py 只是被uwsgi作为一个模块导入的,所以不应该再启动flask内置的web服务器。
响应对象:Response
Response对象是flask中默认使用的响应对象。
手动创建response对象:from?flask?import?Flask,make_response app?=?Flask(__name__)app.config.from_object('config')@app.route('/hello/')def?hello(): ????headers?=?{ ????????'content-type':?'text/plain'????????????????#指定客户端以text格式解析返回内容 ????} ????response?=?make_response('',?404) ????response.headers?=?headers????return?responseif?__name__?==?'__main__': ????app.run(host='0.0.0.0',?port=81,?debug=app.config['DEBUG'])
浏览器访问
可以看到,访问到文本形式的,而且状态码为404,这说明状态码不影响访问返回的内容,只是作为一个标识。
自动创建response对象:from?flask?import?Flask,make_response app?=?Flask(__name__)app.config.from_object('config')@app.route('/hello/')def?hello(): ????headers?=?{ ????????'content-type':?'application/json', ????????'location':?'http://HdhCmsTestbaidu测试数据' ????} ????return?'',?301,?headersif?__name__?==?'__main__': ????app.run(host='0.0.0.0',?port=81,?debug=app.config['DEBUG'])
浏览器访问
可以看到,直接重定向到了百度首页。