Use Content Caching with Vary Headers

A snippet showing how to set Vary headers for caching.


# views.py

from django.views.decorators.vary import vary_on_headers
from django.http import HttpResponse

@vary_on_headers("User-Agent", "Accept-Language")
def news_feed(request):
    return HttpResponse("Vary by UA and language")
      
Explanation:
  • Consider @vary_on_cookie for authenticated vs anonymous content.
  • Downstream caches key the response by specified request headers, avoiding cache poisoning and mismatches.
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.