Using Django View Decorators to Control HTTP Behavior and Caching

This article explores Django’s view decorators, which allow developers to control HTTP methods, enable conditional processing, apply GZip compression, manage Vary headers, configure caching behavior, and customize CommonMiddleware behavior on a per-view basis. These decorators help create more secure, efficient, and predictable Django applications.

Django decoratorsHTTP methodscaching

~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):
    pass

Request methods must be written in uppercase.


Specialized Method Decorators

  • require_GET: Only allows the GET method.
  • require_POST: Only allows the POST method.
  • require_safe: Only allows GET and HEAD methods.

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 using ETag or Last-Modified functions.
  • conditional_page: Applies the behavior of ConditionalGetMiddleware to a specific view.
  • etag: Generates an ETag header for the response.
  • last_modified: Generates a Last-Modified header.

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 supports gzip. It also sets the appropriate Vary header.

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:


  • Expires set to the current date and time
  • Cache-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 when APPEND_SLASH is 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