基于类视图的源码流程

基本使用

路由定义

1 url(r'^register/$', views.RegisterView.as_view(), name='register'),

视图函数中定义

 1 from django.views import View
 2 
 3 class RegisterView(View):
 4 
 5     def dispatch(self, request, *args, **kwargs):
 6         return super().dispatch(request, *args, **kwargs)
 7 
 8 
 9     def get(self, request, *args, **kwargs):
10         return HttpResponse("get")
11 
12     def post(self, request, *args, **kwargs):
13         return HttpResponse("post")

 

 

源码流程

 

 

 

 

views中定义一个装饰器

1 def my_decorator(func):
2     def wrapper(request, *args, **kwargs):
3         print("装饰器执行")
4         print(request.method)
5         return func(request, *args, **kwargs)
6 
7     return wrapper

在url中进行装饰

from app001 import views


url(r'^register/$', views.my_decorator(views.RegisterView.as_view()), name='register'),

 

在视图函数中类视图的方法中进行装饰(对单独的方法进行装饰)

from django.http import HttpResponse
from django.utils.decorators import method_decorator

from django.views import View


def my_decorator(func):
    def wrapper(request, *args, **kwargs):
        print("装饰器执行")
        print(request.method)
        return func(request, *args, **kwargs)

    return wrapper

class RegisterView(View):
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    @method_decorator(my_decorator)
    def get(self, request, *args, **kwargs):
        return HttpResponse("get")

    def post(self, request, *args, **kwargs):
        return HttpResponse("post")

在视图函数中类视图的方法中进行装饰(对类中所有的方法进行装饰);

from django.http import HttpResponse
from django.utils.decorators import method_decorator

from django.views import View


def my_decorator(func):
    def wrapper(request, *args, **kwargs):
        print("装饰器执行")
        print(request.method)
        return func(request, *args, **kwargs)

    return wrapper


@method_decorator(my_decorator, name="dispatch")  # 将装饰器在类中进行装饰
class RegisterView(View):
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        return HttpResponse("get")

    def post(self, request, *args, **kwargs):
        return HttpResponse("post")

将装饰器本身转化为可以适应类视图方法的装饰器

from django.shortcuts import HttpResponse

from django.views import View


def my_decorator(func):
    def wrapper(self, request, *args, **kwargs):  # 此处增加了self
        print('自定义装饰器被调用了')
        print('请求路径%s' % request.method)
        return func(self, request, *args, **kwargs)  # 此处增加了self

    return wrapper


class RegisterView(View):
    @my_decorator
    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        return HttpResponse("get")

    def post(self, request, *args, **kwargs):
        return HttpResponse("post")

类视图的mixin扩展类

url(r'^register/$', views.RegisterView.as_view(), name='register'),

 

from django.http import HttpResponse
from django.views import View


def my_decorator(func):
    def wrapper(request, *args, **kwargs):
        print('自定义装饰器1被调用了')
        print('请求方式%s' % request.method)
        return func(request, *args, **kwargs)

    return wrapper


# 类视图的扩展
class View001Mixin(object):
    @classmethod
    def as_view(cls, **initkwargs):
        view = super().as_view(**initkwargs)
        view = my_decorator(view)
        return view


def my_decorator2(func):
    def wrapper(request, *args, **kwargs):
        print('自定义装饰器2被调用了')
        print('请求路径%s' % request.path)
        return func(request, *args, **kwargs)

    return wrapper


class View002Mixin(object):
    @classmethod
    def as_view(cls, **initkwargs):
        view = super().as_view(**initkwargs)
        view = my_decorator2(view)
        return view


class RegisterView(View001Mixin, View002Mixin, View):
    # def dispatch(self, request, *args, **kwargs):
    #     return super().dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        return HttpResponse("get")

    def post(self, request, *args, **kwargs):
        return HttpResponse("post")

 

mro的三种继承顺序:

1,深度优先(经典类);

2,广度优先(新式样);

3,C3算法(3.5以及其他版本之上)。

 

posted @ 2018-07-11 16:44  cerofang  阅读(125)  评论(0编辑  收藏  举报