zoukankan      html  css  js  c++  java
  • flask2

    闪现:多用于返回报错信息

    from flask import Flask,request,render_template,redirect,url_for,get_flashed_messages,flash
    app = Flask(__name__)
    app.debug = True # 调试模式
    app.secret_key="dsada"
    @app.route("/",methods=["GET"])
    def index():
    flash('aaaaaaaaa')
    return "bbbbbbbbbbb"

    @app.route("/flash_test")
    def flash_test():
    a = get_flashed_messages()
    print(a)
    return "test"

    if __name__ == '__main__':
    app.run()

    # flash("值",category=“分类名”)

    请求扩展:

    ★1.@app.before_request

    def before()

      pass

    请求到来之前

    ★2.@app.before_request

    def after(response)

      return response

    请求之后

    3.@app.before_first_request

    def first():

      pass

    第一次请求时,跟浏览器无关

    4.@app.teardown_request

    def ter(e):

      print(e)

    如果没有报错信息,e为none,当有报错则会e会显示报错信息

    每一个请求之后绑定一个函数,即使遇到了异常

    当app.debug  = True,ter函数不会执行,报错信息会显示在前端页面上,当没有debug,报错信息显示在后台

    5.@app.errorhandler(404)

    def error(msg):

      print(msg)

      return '404错误'

    服务器内部错误

    6.@app.template_global()

    def sb(a1,a2):

      return a1+a2

    标签{{sb(a1,a2)}}

    中间件:

    服务器中间件

    数据库中间件

    消息中间件

    app.Flask(__name__,template_folder='template',static_folder='static',static_url_path='/static') 

    蓝图对象,代替了app对象

    account = Blueprint('account',__name__)

    注册蓝图:

    app.register_blueprint(user)

    请求上下文源码分析:

    0:实例化产生全局变量

      request_ctx_stack = LocalStack()

        LocalStack()内部包了local对象,self._local = Local()

    1:如果请求来了

      调用app.__call__也就是执行了self.wsgi_app(environ,start_response)

      ctx = self.request_context(environ)

        生成一个ctx对象,内部包含了request,session。。。

      ctx.push()

        requestcontext类的push方法,内部看_request_ctx_stack.push(self)

        localstack对象的push方法把ctx传过去了

        localstack对象的push内部干的事:

          通过反射,取local对象的内部stack属性,一开始没有

          self._local.stack = rv = []

          self._local.stack = [] 在调用local对象的__setattr__方法

          生成了{‘线程id’:{‘stack’:[ ]}}

          把ctx对象放到了列表中:{‘线程id’:{‘stack’:[ ]}}

           多个线程或者协程的话{‘线程id’:{‘stack’:[ ]}}{‘线程id’:{‘stack’:[ ]}}

    2.执行response = self.full_dispatch_request():根据路由执行视图函数

      self.try_trigger_before_first_request_functions()

        执行before_first_request装饰器装饰的函数,一旦执行过一次,以后再也不执行了,就是一个标志

      -rv = self.preprocess_request():执行所有的用before_request装饰的函数

      -rv = self.dispatch_request():根据路由执行视图函数      -----视图函数执行

    3 最终:ctx.auto_pop(error)
       -rv = _request_ctx_stack.pop()

      

    Request对象:
    -本质是LocalProxy对象
      -Request对象在实例化的时候:request = LocalProxy(partial(_lookup_req_object, 'request'))
      -def _lookup_req_object(name):
        #传了个request过来
        #_request_ctx_stack.top就是去local对象中取出ctx(最顶部的ctx)
    top = _request_ctx_stack.top
    if top is None:
      raise RuntimeError(_request_ctx_err_msg)
    #ctx是RequestContext对象取request属性,就是Request对象
    return getattr(top, name)

      -在视图函数中打印request,调用request的__str__方法,LocalProxy的__str方法

    偏函数:

    def add(x,y,z):

      return x+y+z

    add_par = partial(add,1)

    print(add_par(2,3)) ====6

    提前把变量传过来

    g对象:

    global

    专门用来存储用户信息的g对象,g对象在一次请求中所有的代码的地方都可以使用

    g对象和session的区别

    session对象可以跨request的,只要session还未失效,不同的request的请求会获取到同一个session,但是g对象不是,g对象不需要管过期时间,请求一次g对象就改变了一次,或者重新赋值了一次

    ctx和app_ctx
      ctx包含了request和session
      app_ctx包含了g和current_app

  • 相关阅读:
    BZOJ3065(替罪羊树套线段树)
    BZOJ3052(树上带修莫队)
    BZOJ1095(动态点分治+堆)
    NOIWC颓废记
    BZOJ2125 最短路
    Simpson积分(BZOJ2178)
    BZOJ4555 [Tjoi2016&Heoi2016]求和
    NTT+多项式求逆+多项式开方(BZOJ3625)
    Miller-Rabin,Pollard-Rho(BZOJ3667)
    单纯形求解线性规划(BZOJ1061)
  • 原文地址:https://www.cnblogs.com/xuezhihao/p/11393548.html
Copyright © 2011-2022 走看看