← Back to Django Course | Chapter 2: Apps, URLs & Views | Lesson 10 of 10

View Decorators Basics

A decorator is like a security guard you put in front of a view — it checks something (like which HTTP method was used) before letting the view run.

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.

markup
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.

markup
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.

markup
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. #}
Common Mistakes
  1. Applying @require_POST to a view that also needs to handle GET requests, causing all GET requests to fail.
  2. Forgetting to import the decorator from django.views.decorators.http before using it.
  3. Stacking decorators in the wrong order, which can change how they interact with the view function.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.