flask相关总结1

1.环境配置,

python -V pip -V

推荐使用 pipenv

pipenv shell 激活环境

pipenv --venv 查看虚拟环境路径

pipenv graph 安装包依赖关系

 

2.最小原型

from flask import Flask

app = Flask(__name__)

app.run()

 

3.唯一URL原则

注册视图函数方法一

@app.route('/hello')===1

@app.route('hello')===2

url为 /hello 一次请求调用一次返回或者 /hello/两次请求调用两次返回第一次为重定向301

基于 seo考虑,用2的情况会被收录两次

除了 视图函数 还有 基于类的视图,暂时不描述

注册视图函数方法二

app.add_url_rule('/hello/',view_func=hello)

 

4.启动方法

app.run(host='0.0.0.0',debug=True,port=81)

 

5.配置文件生成

一:创建 config.py文件在目录中直接from import

二:实例化app中的config对象

app.config.from_object('config')

读取 app.config['DEBUG']

这里必须注意的是config.py文件中key值必须都是大写的

 

6.响应

视图函数的响应都会被框架包装成为response

含有statecode content-type 等信息

headers= {'content-type':'text/html','location':'www.baidu.com'}

response = make_response('<b>hellos</b>',404)

response.headers = headers

return response

或者

return '<b>hello2</b>',404,headers

content-type = 'text/html'

application/json

text/plain

etc

posted @ 2018-08-28 11:41  billhsu  阅读(116)  评论(0编辑  收藏  举报