zoukankan      html  css  js  c++  java
  • Flask配置文件

    昨日回顾:
        1 什么是celery:执行异步任务,和定时任务
            -架构
                -broker:消息中间人,redis,rabbitmq
                -worker:执行者(开多个)
                -backend:结果存储,redis
            -异步任务:
                提交任务到broker中:
                    -函数.delay(参数)
                    -函数.apply_async(args=[4, 3], eta=task_time)
                启动worker来执行
            -定时任务:
                -延迟执行
                -指定时间点循环执行
                    -beat:调度
                    -启动work
                    
            
        2 公司开发环境
            -windows平台下开发
            -乌班图
            -mac
        3 Flask
            -python中几大框架:djang,flask,tornado,web.py
            -pip3 install flask
            -三件套:返回字符串,返回重定向,返回页面,返回json格式:jsonify
            -模板:
                1 可以加括号执行函数,传参数
            -作业
                -endpoint 如果没指定,默认会以函数名作为别名
            
            
    
    今日内容
        -反向解析
            url=url_for('index')
            url=url_for(别名,endpoint指定的别名)
        
        -配置信息
            -0 app.debug,app.secret_key
            -1 app.config['DEBUG'] = True
            -2 通过文件,json,字典,类配置
                -app.config.from_pyfile("python文件名称")---推荐
                -app.config.from_json("json文件名称")
                -app.config.from_mapping({'DEBUG': True})
                -app.config.from_object("python类或类的路径")---推荐
            -路径配置
                instance_path=路径,
                instance_relative_config=True,
                以后settings.py文件放在这个路径下
        -路由系统
            -典型写法
                -@app.route('/index',methods=['GET'],endpoint='index')
            -内置转换器
                -string
                -int
            -路由本质学到的
                -如果endpoint不传,默认用函数名字view_func.__name__
                -flask中的路由,是基于装饰器,但本质是:add_url_rule(rule, endpoint, f, **options)
                -可以这么写app.add_url_rule('/test', 'test',test)
    
            -CBV介绍
                -写法
                    class CBVTest(views.MethodView):
                        # 需要配置能够处理的请求方式
                        methods=['GET','POST']
                        def get(self):
                            return 'cbv_get'
                        def post(self):
                            return 'cbv-post'
    
                    app.add_url_rule('/cbvtest',view_func=CBVTest.as_view(name='cbvtest'))
                -加装饰器:
                    decorators=[login_auth,]  写在前面,装饰器在最里层
                    -内部循环decorators,包裹as_view返回值(viwe函数)
            -add_url_rule函数参数详解(了解)
                -defaults={'id':1}
                -#对URL最后的 / 符号是否严格要求
                    strict_slashes = None
                -#重定向到指定地址
                    redirect_to = None, 
                -#子域名访问
                    subdomain = None, 
            -支持正则表达式,自定义转换器
                -#1 写类,继承BaseConverter
                #2 注册:app.url_map.converters['regex'] = RegexConverter
                # 3 使用:@app.route('/index/<regex("d+"):nid>')  正则表达式会当作第二个参数传递到类中
            
    模板:
        -支持函数加括号执行,支持传参
        -flask处理了xss攻击,如果想显示原生html
            -safe:模板中
            -Markup:后台处理
            
    请求响应:
        请求:
            # request.method
            # request.args
            # request.form
        响应:
            # return "字符串"
            # return render_template('html模板路径',**{})
            # return redirect('/index.html')
            #return jsonify({'k1':'v1'})
        如果加cookie,往响应头中写东西
            # from flask import make_response
            # response = make_response(render_template('index.html'))
            # response是flask.wrappers.Response类型
            # response.delete_cookie('key')
            # response.set_cookie('key', 'value')
            # response.headers['X-Something'] = 'A value'
            # return response
    session:
        设置值:session['user']='lqz'
        删除值:
            session.pop('username', None)
            del ession['user']
        取值:session['user']
        
    闪现(message)
        -设置:flash('aaa')
        -取值:get_flashed_message()
        -假设在a页面操作出错,跳转到b页面,在b页面显示a页面的错误信息
        -分类存
            flash('你的名字不是lqz是%s',category='aa')
        -分类取
            get_flashed_messages(category_filter=['aa',])
            
    请求扩展
         -before_request
         -after_request
         -before_first_request
    
            
    作业:
        -看django源码,如何实现的如果路径中不带/ ,自动加上 /
            from django.middleware.common import CommonMiddleware
        -登录认证(before_request)
  • 相关阅读:
    Educational Codeforces Round 83 --- F. AND Segments
    Educational Codeforces Round 83 --- G. Autocompletion
    SEERC 2019 A.Max or Min
    2019-2020 ICPC Southwestern European Regional Programming Contest(Gym 102501)
    Educational Codeforces Round 78 --- F. Cards
    今天我学习了一门全新的语言
    codeforces 1323D 题解(数学)
    Educational Codeforces Round 80 (Div. 2) 题解 1288A 1288B 1288C 1288D 1288E
    Educational Codeforces Round 81 (Div. 2) 题解 1295A 1295B 1295C 1295D 1295E 1295F
    Codeforces Round #617 (Div. 3) 题解 1296C 1296D 1296E 1296F
  • 原文地址:https://www.cnblogs.com/xuqidong/p/13124282.html
Copyright © 2011-2022 走看看