基于类的视图,以及url 路由解析
jumpserver 这个轮子是好跑的轮子,又大又圆,对源码进行解析。
jumpserver 中 用了 大量的基于类的视图函数,相对抽象一点,隐藏了一些细节。大量使用 类似下面的url路由。
urlpatterns = [
url(r'^v1/assets-bulk/$', api.AssetListUpdateApi.as_view(), name='asset-bulk-update')]
跟 基于函数的视图不一样, 配置类视图的时候,需要使用类视图的as_view()方法来注册添加,
as_view 方法 是django 内置base 类中,view类的 类方法。下面贴出源码
class View:
"""
Intentionally simple parent class for all views. Only implements
dispatch-by-method and simple sanity checking.
"""
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
def __init__(self, **kwargs):
"""
Constructor. Called in the URLconf; can contain helpful extra
keyword arguments, and other things.
"""
# Go through keyword arguments, and either save their values to our
# instance, o