Class view using

Django also uses classes to define a view, called a class view.

Using a class view allows you to define different requests for a view using different methods in the class. As shown below.

from django.views.generic import View

class RegisterView(View) :
    """ Class view: Handling registration """

    def get(self, request) :
        """ Process the GET request and return to the registration page """
        return render(request, 'register.html')

    def post(self, request) :
        """ Handle POST requests, implement registration logic """
        return HttpResponse('Registration logic is implemented here')
Copy the code

Benefits of class view:

  • Good code readability
  • Class views are more reusable than function views, so if a particular logic of a class view is needed elsewhere, you can simply inherit from that class view

Define the class View we need to inherit from the Django offers the parent of a View, can be used from the Django. Views. The generic import the View or from the Django. Views. Generic. The base import the View to import, The definition is shown above.

When configuring routes, add them using the as_view() method of the class view.

urlpatterns = [
    # View function: register
    # url(r'^register/$', views.register, name='register'),
    Class view: Register
    url(r'^register/$', views.RegisterView.as_view(), name='register'),]Copy the code

Principles of class View

@classonlymethod
    def as_view(cls, **initkwargs) :
        """ Main entry point for a request-response process. """. Omit code...def view(request, *args, **kwargs) :
            self = cls(**initkwargs)
            if hasattr(self, 'get') and not hasattr(self, 'head'):
                self.head = self.get
            self.request = request
            self.args = args
            self.kwargs = kwargs
            Call dispatch method, call different request methods according to different request methods
            returnself.dispatch(request, *args, **kwargs) ... Omit code...Return the real function view
        return view


    def dispatch(self, request, *args, **kwargs) :
        # Try to dispatch to the right method; if a method doesn't exist,
        # defer to the error handler. Also defer to the error handler if the
        # request method isn't on the approved list.
        if request.method.lower() in self.http_method_names:
            handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
        else:
            handler = self.http_method_not_allowed
        return handler(request, *args, **kwargs)
Copy the code

Class view’s multiple inheritance overrides Dispatch

class CenterView(View) :

    def get(self,request) :
        return HttpResponse("OK")

    def post(self,request) :
        return HttpResponse("OK")
Copy the code

Use object-oriented multiple inheritance features.

class CenterView(LoginRequireMixin,View) :

    def get(self,request) :
        return HttpResponse("OK")

    def post(self,request) :
        return HttpResponse("OK")
Copy the code