Wellcom to my Blog for Javascript

flask添加视图函数的方式

在flask中添加视图的方式如下

这种方式通过app直接调用方式将URL映射到此函数上面。最常用到方式

1 def add_url_rule(
2         self,
3         rule,
4         endpoint=None,
5         view_func=None,
6         provide_automatic_options=None,
7         **options
8     )

 

这种方式是通过装饰器来使用路由注册功能

1 def route(self, rule, **options):
 1     def endpoint(self, endpoint):
 2         """A decorator to register a function as an endpoint.
 3         Example::
 4 
 5             @app.endpoint('example.endpoint')
 6             def example():
 7                 return "example"
 8 
 9         :param endpoint: the name of the endpoint
10         """
11 
12         def decorator(f):
13             self.view_functions[endpoint] = f
14             return f
15 
16         return decorator

 

这种方式是注册endpoints功能,将指定的函数注册成endpoint。这种方式的内部其实还是调用了view_function将函数注册成视图函数。

 1     def endpoint(self, endpoint):
 2         """A decorator to register a function as an endpoint.
 3         Example::
 4 
 5             @app.endpoint('example.endpoint')
 6             def example():
 7                 return "example"
 8 
 9         :param endpoint: the name of the endpoint
10         """
11 
12         def decorator(f):
13             self.view_functions[endpoint] = f
14             return f
15 
16         return decorator

 

这种方式是将指定的函数注册成视图函数。以方便使用

self.view_functions = {}

 

posted on 2021-01-04 13:50  温柔的鲨鱼  阅读(162)  评论(0编辑  收藏  举报

导航