• Flask知识总结


    1.-----------------路由设置的2种方式-----------------

        查看源码,route方法里,本质是执行app.add_url_rule()

        因此可以这么写(主流方式):

                @app.route("/xxx")

                def index():

                    return "index!"

        还可以这么写:

                def index():

                    return "index!"

                app.add_url_rule("/xxx", None, index)

        flask源码定义:

    def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
    if endpoint:
    assert "." not in endpoint, "Blueprint endpoints should not contain dots"
    if view_func and hasattr(view_func, "__name__"):
    assert (
    "." not in view_func.__name__
    ), "Blueprint view function name should not contain dots"
    self.record(lambda s: s.add_url_rule(rule, endpoint, view_func, **options))

    2.-----------------route有很多的参数!-----------------

       1)重定向 redrict_to

       @app.route("/index", redrict_to="/new")

       def index():

           pass 

       @app.route("/new", redrict_to="/new")

       def new():

           pass

    3.-----------------路由系统 (本质是把url和func搞一个对应关系)-----------------

    @app.route("/index", methods=["GET", "POST"], endpoint="n1")

    endpoint 相当于反向生成,不写就默认是函数名

    翻页传参

    @app.route("/index/<int:id>", methods=["GET", "POST"])

    def index(id):

        pass

    4.-----------------上下文管理-----------------

    三个对象

    localProxy类,作用:代理,取值。

    request = localProxy("request")

    print(request.method)

    session = localProxy("session")

      请求流程:

        阶段一.请求到来:将request和session相关数据封装到ctx对象中,再通过localStack将ctx添加的Local中。

        阶段二.视图函数中获取request或者session:

                 方式1  直接从localStack中获取

                 方式2  通过localProxy获取, request是一个localProxy对象!

    -------- g 和 session ---------

    g的生命周期  每个http请求创建一个g,请求走了就销毁了。 同一个请求中可以在各个阶段中使用g!

    g用来做点啥?可以权限系统!

    app有啥用?

    读配置文件 app.config

  • 相关阅读:
    Python while循环实现重试
    VBA find查找行号和列号的方法
    通过selenium控制浏览器滚动条
    【转】自然语言处理P,R,F值的计算公式
    【转】ultraedit 正则表达式
    【转】java文件输出流,写到.txt文件,如何实现换行
    Java heap space 解决方法
    XML+RDF——实现Web数据基于语义的描述(转载)
    java学习笔记——jsp简单方法读取txt文本数据
    一个完全独立的今天
  • 原文地址:https://www.cnblogs.com/yuzhaoblog/p/12254677.html
Copyright © 2020-2023  润新知