flask01

作者: fly5 | 来源:发表于2018-10-20 15:15 被阅读0次

    一、flask基本使用

    • 安装flask
    pip install flask
    
    • 创建项目

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

    @app.route('/hello/')
    def hello():
        return 'hello flask'
    
    • 启动项目
    python app.py
    

    二、flask-script插件

    • 作用
    用于接收命令行参数!!!
    
    • 官方文档
    https://flask-script.readthedocs.io/en/latest/
    
    • 安装插件
    pip install Flask-Script
    
    • 实例化(初始化配置)
    from flask_script import Manager
    manager = Manager(app)
    
    • 使用
     manager.run()
    
    • 说明
    python manager.py runsderver    # 基本操作,启动项目
    python manager.py runsderver --help # 查看帮助
    python manager.py runsderver -h HOST        # 主机名
    python manager.py runsderver -p PORT        # 端口号
    python manager.py runsderver -d         # 开启调试模式(默认是关闭的)
    python manager.py runsderver -r         # 自动重新加载(默认是不重新加载)
    
    python manager.py runserver -p 7000 -h '0.0.0.0' -r -d
    python manager.py runserver -r -d
    

    三、flask-blueprint插件

    • 蓝图作用
    主要用来规划urls(路由)
    
    • 官网
    http://flask.pocoo.org/docs/0.12/blueprints/
    
    • 安装
    pip install flask-blueprint
    
    • 初始化配置
    # 实例化对象
    blue = Blueprint('simple_page', __name__)
    
    • 使用
    @blue.route('/hello/')
    def hello():
        return 'hello flask!'
    

    四、视图之request

    • 请求参数
    路径参数、get请求参数、post请求参数
    
    • 路径参数
    格式: <类型:变量名>
    例如: <string:name>
    类型: int/string/float/uuid/any/path
    

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

    • 请求方式
    GET/POST/DELETE....
    

    请求测试工具: Linux-postman、Windows-postman、Mac-postman、谷歌浏览器-postman插件、火狐浏览器-RESTClient插件、pycharm-tools(RESTClient)

    • 反向解析
    格式: url_for('蓝图名.函数名')
    例如: path = url_for('blueapp.hello')
    

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

    • request对象
      服务器接受到客户端请求后,会自动创建Request对象,不能修改! 【全局内置对象,所有函数都有】
    requeset.method 请求方式
    requeset.path   请求路径
    request.url     请求url
    request.base_url    基础url
    request.agrs    get请求参数
    request.form    post请求参数
    request.files   文件参数
    request.headers 请求头
    request.cookies  cookie内容
    

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

    五、视图之response

    • response创建
      手动创建,服务器返回给客户端的!
    - 直接返回字符串
    - 返回模板render_templated  [也是字符串]
    - make_resnponse()  【response对象】
    - Response()        【response对象】
    
    • 返回配置
    2xx: 成功
    3xx: 重定向
    4xx: 客户端错误
    5xx: 服务端错误
    
    return 'hello!!!',555
    response = make_response('试试看,可以不', 208)
    response = Response('响应', 403)
    
    • 异常处理
    ## 抛出异常
    @blue.route('/errtest/')
    def errtest():
        # 抛出异常
        # abort(403)
        abort(404)
        return '能否显示出来?'
    
    # 异常捕获
    @blue.errorhandler(404)
    def handler404(ex):
        return '我绝对不是404!'
    

    六、会话技术之cookie

    • 概述
    - 会话技术(状态保持,HTTP短链接,HTTP无状态)
    - cookie 客户端会话技术
    - cookie 数据是全部保存在客户端
    - cookie 存储方式key-value
    - cookie 支持过期
    - cookie 默认会自动携带本站点的所有cookie
    
    • 方法
    - 设置cookie
        response.set_cookie(key, value)
    
    - 获取cookie
        request.cookies.get(key)
    
    - 删除cookie
        response.delete_cookie(key)
    

    十、作业

    首页
        登录、退出、用户名(有登录、没登录)
    
    购物车
        用户名(有登录、没登录)
    
    登录
        用户名输入框
        登录按钮
    

    相关文章

      网友评论

          本文标题:flask01

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