from django.http import HttpResponse
def custom_decorator(view_func):
def wrapper(request, args, *kwargs):
# Code to execute before the view function is called
result = view_func(request, args, *kwargs)
# Code to execute after the view function is called
return result
return wrapper
@custom_decorator
def my_view(request):
# View logic here
return HttpResponse("Hello, Django!")
- Import the
HttpResponse
class from the django.http
module. - Define a custom decorator function named
custom_decorator
that takes a view_func
as an argument. - Inside the decorator, define a nested function named
wrapper
that takes request
, args
, and *kwargs
as parameters. - Implement code before the actual view function (
view_func
) is called. - Call the original view function (
view_func
) with the provided parameters and store the result in the result
variable. - Implement code to execute after the view function is called.
- Return the result obtained from the original view function.
- Decorate the
my_view
function with the @custom_decorator
syntax to apply the custom decorator to the view.