~3 min read • Updated Mar 14, 2026
Introduction
Django provides a variety of view decorators that allow developers to control how views respond to different HTTP behaviors. These decorators can restrict allowed request methods, manage caching, apply compression, and modify response headers without altering the core logic of the view itself.
Restricting Allowed HTTP Methods
The module django.views.decorators.http includes decorators that restrict access to views based on the request method. If the request method is not allowed, Django returns an HttpResponseNotAllowed.
Using require_http_methods
This decorator ensures that a view only accepts specific HTTP methods:
from django.views.decorators.http import require_http_methods
@require_http_methods(["GET", "POST"])
def my_view(request):
passRequest methods must be written in uppercase.
Specialized Method Decorators
require_GET: Only allows theGETmethod.require_POST: Only allows thePOSTmethod.require_safe: Only allowsGETandHEADmethods.
The HEAD method is typically handled the same as GET, except that the response body is removed. Because tools like link checkers rely on HEAD, require_safe is often preferred over require_GET.
Conditional View Processing
Django provides decorators for conditional request handling and cache optimization:
condition: Enables conditional processing usingETagorLast-Modifiedfunctions.conditional_page: Applies the behavior ofConditionalGetMiddlewareto a specific view.etag: Generates anETagheader for the response.last_modified: Generates aLast-Modifiedheader.
These decorators help reduce server load by enabling efficient caching and conditional responses.
GZip Compression
The module django.views.decorators.gzip provides decorators for enabling GZip compression on a per-view basis.
gzip_page: Compresses the response if the client supportsgzip. It also sets the appropriateVaryheader.
Using GZip compression can significantly reduce response size and improve performance.
Managing Vary Headers
The Vary header tells caches which request headers should be considered when generating cache keys. Django provides decorators to control this behavior:
vary_on_cookie: Varies the cache based on cookies.vary_on_headers: Varies the cache based on specific request headers.
These decorators are useful when serving dynamic content that depends on user-specific or header-specific data.
Caching Control
The module django.views.decorators.cache includes decorators for controlling server-side and client-side caching.
cache_control
This decorator modifies the Cache-Control header by adding the provided keyword arguments.
never_cache
This decorator ensures that a response is never cached by adding the following headers:
Expiresset to the current date and timeCache-Control: max-age=0, no-cache, no-store, must-revalidate, private
Each header is added only if it is not already present.
Customizing CommonMiddleware Behavior
The module django.views.decorators.common allows per-view customization of CommonMiddleware behavior.
no_append_slash: Prevents automatic slash-appending behavior whenAPPEND_SLASHis enabled.
Conclusion
Django’s view decorators provide powerful tools for controlling request behavior, caching, compression, and middleware interactions. By using these decorators effectively, developers can create more secure, efficient, and predictable applications while keeping view logic clean and maintainable.
Written & researched by Dr. Shahin Siami