码迷,mamicode.com
首页 > 其他好文 > 详细

Flask知识总结

时间:2020-02-03 11:53:50      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:and   get   反向   edr   相关   视图   func   session   cal   

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

 

Flask知识总结

标签:and   get   反向   edr   相关   视图   func   session   cal   

原文地址:https://www.cnblogs.com/yuzhaoblog/p/12254677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!