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:
The following code is exactly the same as the preceding:
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.