Decorators

Before class-based views, decorators were the only way to change the behavior of function-based views.

Being wrappers around a function, they cannot change the inner working of the view, and thus effectively treat them as black boxes.

A decorator is a function that takes a function and returns the decorated function.

Use the annotation notation @, as shown in the following login_required decorator example:

@login_required
def simple_view(request):
    return HttpResponse()

The following code is exactly the same as the preceding:

def simple_view(request):
    return HttpResponse()

simple_view = login_required(simple_view)

Decorators are less flexible than mixins. However, they are simpler. You can use both decorators and mixins in Django. In fact, many mixins are implemented with decorators.