View Decorators Basics
What a Decorator Does
A decorator is a Python function that wraps another function to add extra behavior without changing its code. Django ships several decorators for common view concerns, like restricting HTTP methods.
Example: What a Decorator Does
A decorator is a Python function that wraps another function to add extra behavior without changing its code. Django ships several decorators for common view concerns, like restricting HTTP methods.
from django.views.decorators.http import require_GET
from django.http import HttpResponse
@require_GET
def home(request):
return HttpResponse('GET-only view')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Restricting to Specific Methods
require_http_methods takes a list of allowed HTTP verbs; any other verb gets an automatic 405 Method Not Allowed response, so the view body never has to check request.method itself.
Example: Restricting to Specific Methods
require_http_methods takes a list of allowed HTTP verbs; any other verb gets an automatic 405 Method Not Allowed response, so the view body never has to check request.method itself.
from django.views.decorators.http import require_http_methods
from django.http import HttpResponse
@require_http_methods(['GET', 'POST'])
def contact(request):
return HttpResponse('Contact page')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Stacking Multiple Decorators
Decorators can be stacked; Python applies them bottom-up, so the decorator closest to the function runs first when the view is called.
Note: Order matters — read stacked decorators from the bottom up to understand execution order.
Example: Stacking Multiple Decorators
Decorators can be stacked; Python applies them bottom-up, so the decorator closest to the function runs first when the view is called.
from django.views.decorators.http import require_GET
from django.views.decorators.cache import never_cache
@never_cache
@require_GET
def dashboard(request):
return HttpResponse('Live dashboard data')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Applying @require_POST to a view that also needs to handle GET requests, causing all GET requests to fail.
- Forgetting to import the decorator from django.views.decorators.http before using it.
- Stacking decorators in the wrong order, which can change how they interact with the view function.
- Decorators wrap a view function to add behavior, like restricting allowed HTTP methods.
- @require_http_methods, @require_GET, and @require_POST restrict which HTTP verbs a view accepts.
- Decorators are applied directly above the view function definition using the @ syntax.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: