美文网首页
02-flask 基本使用

02-flask 基本使用

作者: 学飞的小鸡 | 来源:发表于2018-10-20 15:11 被阅读0次
  • 安装flask
pip install flask

  • 创建项目

  • 添加路由规则和视图函数


# 字符串

@app.route('/hello/')

def home():

return 'hello world'

# 模板

@app.route('/home/')

def home():

return render_template('home.html')

  • 启动项目

python app.py

五 flask-script 插件

  • 作用

用于命令行接收参数

  • 官方文档
[https://flask-script.readthedocs.io/en/latest/](https://flask-script.readthedocs.io/en/latest/)
  • 步骤

  • 安装插件


pip install Flask-script

  • 实例化

manager = Manager(app)

  • 使用

manager.run()

  • 说明

python manager.py runserver # 基本操作,启动项目

python manager.py runserver -- help    # 查看帮助

python manager.py runserver -h HOST  # 主机名

python manager.py runserver -p  PORT  # 端口号

python manager.py runserver -d  # 开启debug

python manager.py runserver -r # 重新加载

六 flask-blueprint 插件

  • 蓝图作用

主要用来规划urls

  • 官网

[http://flask.pocoo.org/docs/1.0/blueprints/](http://flask.pocoo.org/docs/1.0/blueprints/)

  • 步骤

  • 安装


pip install flask-blueprint

  • 初始化配置

  • 实例化对象(在views中)


blue  = Blueprint('simple_page', __name__)

  • 注册(在init)中

app.register_blueprint(blueprint = blue)

七 、视图之request

  • 请求参数

路劲参数、get请求参数、post请求参数

  • 路径参数

路径:<类型:变量名>

例如:<string:name>

类型:int/string/float/uuid/any/path

路由参数名和视图函数参数名,应该一一对应

  • 请求方式

GET/POST/DELETE.....

默认支持GET、HEAD、OPTIONS

请求测试工具:谷歌:Linux-postman、Windows-postman、Mac-postman

火狐: 火狐流浪器-RESTClient插件

pycha-tools

  • 反向解析

格式: url_for('蓝图名.函数名')

例如: path = url_for('blueapp.hello')

重定向: redirect('/index/')

  • request 对象

服务器接收到客户端请求后,会自动创建Request对象,不能被修改!【全局变量】


@blue.route('/requestsettest/',methods=['GET','POST'])

def requestsettest():

# 请求方式

if reques.method == 'GET':

return 'get请求'

elif request.method == 'POST':

return 'post请求'

request.method    请求方式

request.path        请求路径

request.url            请求url

request.args        get 请求参数

request.form        post请求参数

request.files          文件参数

request.headers  请求头

request.cookies    cookie内容

ImmutableMultiDict 类字典

request.args.get('age') 不存在返回None【推荐使用】

八、 视图之response

  • response创建

手动创建,服务器返回客户端的!

- 直接返回字符串

- 返回模板render_template    [也是字符串]

- make_response()    [response对象]

- Response()      [response对象]

response = Response('响应', 208)

return response

  • 返回配置

2xx: 成功

3xx: 重定向

4xx: 客户端错误

5xx:  服务器错误

return 'hello',555

response = make_response('响应', 208)

response = Response('响应', 208)

九、会话技术之cookie

  • 概述

- 会话技术(状态保持, HTTP短链接, HTTP无状态)

- cookie 客户端会话技术

- cookie 数据是全部保存在客户端

- cookie 存储方式key-value

- cookies 支持过期

- cookie 默认会自动携带本站点的所有cookie

  • 方法

- 设置cookie

response.set_cookie(key,value)

- 获取cookie

request.cookies.get(key)

- 删除cookie

response.delete_cookie(key)

相关文章

  • 02-flask 基本使用

    安装flask 创建项目 添加路由规则和视图函数 启动项目 五 flask-script 插件 作用 官方文档 步...

  • Flutter--Text/Container/Image

    Text基本使用 Container基本使用 Image基本使用

  • 基本使用

    1、 打开需要上传的文件夹执行: git init 格式化窗口 2、执行 git add . 上传文件 3、执行 ...

  • 基本使用

    href="javascript:;" 其中javascript: 是一个伪协议。它可以让我们通过一个链接来调用...

  • 基本使用

    数据库: 什么是数据库?简单来说就是存数据的。 都有什么是数据库? oracle(强大,跟金融政府打交道的,安全,...

  • 基本使用

    本文参考:https://morvanzhou.github.io/tutorials/machine-learn...

  • 02-Flask之模板&模型

    一、模板 模板概念 Jinja2 模板语法 结构标签 block 静态资源的加载 结构标签 extends 结构标...

  • 6-xpath和css select基本使用

    Xpath基本使用 css select基本使用

  • MySQL语法入门(一)

    MySQL语法入门(一) 基本运算符使用 基本数学函数使用 基本字符串函数使用 基本日期时间函数使用

  • python time与datetime模块基本使用

    time模块基本使用 datetime模块基本使用

网友评论

      本文标题:02-flask 基本使用

      本文链接:https://www.haomeiwen.com/subject/ggmyaftx.html