SoFunction
Updated on 2024-10-29

Explaining the use of decorators for class views in Django in detail

Class views use decorators

Adding a decorator to a class view can be done using two methods.

For ease of understanding, let's first define a decorator for function views (basically, function views are considered as decorated objects when designing decorators), and a class view to be decorated.

def my_decorator(func):
  def wrapper(request, *args, **kwargs):
    print('The custom decorator was called')
    print('Request path %s' % )
    return func(request, *args, **kwargs)
  return wrapper

class DemoView(View):
  def get(self, request):
    print('get method')
    return HttpResponse('ok')

  def post(self, request):
    print('post method')
    return HttpResponse('ok')

4.1 Decorating in URL Configuration

urlpatterns = [
  url(r'^demo/$', my_decorate(DemoView.as_view()))
]

This approach is the simplest, but because the decoration behavior is placed in the url configuration, when you look at the view alone, you can not know that the view has also been added to the decorator, which is not conducive to the integrity of the code, it is not recommended to use.

This approach adds decorator behavior to all request methods in the class view (as it is at the view entry, before distributing the request methods).

4.2 Decorating in the class view

When using a decorator prepared for a function view in a class view, you cannot add the decorator directly; you need to use method_decorator to convert it to a decorator that applies to the class view's methods.

The method_decorator decorator uses the name parameter to specify the method to be decorated.

# Add decorators to all request methods
@method_decorator(my_decorator, name='dispatch')
class DemoView(View):
  def get(self, request):
    print('get method')
    return HttpResponse('ok')

  def post(self, request):
    print('post method')
    return HttpResponse('ok')


# Add decorators to request-specific methods
@method_decorator(my_decorator, name='get')
class DemoView(View):
  def get(self, request):
    print('get method')
    return HttpResponse('ok')

  def post(self, request):
    print('post method')
    return HttpResponse('ok')

If you need to add decorators to multiple methods of a class view, but not all of them (refer to the above example for adding decorators to all methods), you can use method_decorator directly on the methods that need to have decorators added, as follows

from  import method_decorator

# Add decorators to request-specific methods
class DemoView(View):

  @method_decorator(my_decorator) # Added decorator to get method
  def get(self, request):
    print('get method')
    return HttpResponse('ok')

  @method_decorator(my_decorator) # Added decorator for the post method
  def post(self, request):
    print('post method')
    return HttpResponse('ok')

  def put(self, request): # No decorator added to the put method
    print('put method')
    return HttpResponse('ok')

This is the entire content of this article.