You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vary.py 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from functools import wraps
  2. from django.utils.cache import patch_vary_headers
  3. def vary_on_headers(*headers):
  4. """
  5. A view decorator that adds the specified headers to the Vary header of the
  6. response. Usage:
  7. @vary_on_headers('Cookie', 'Accept-language')
  8. def index(request):
  9. ...
  10. Note that the header names are not case-sensitive.
  11. """
  12. def decorator(func):
  13. @wraps(func)
  14. def inner_func(*args, **kwargs):
  15. response = func(*args, **kwargs)
  16. patch_vary_headers(response, headers)
  17. return response
  18. return inner_func
  19. return decorator
  20. def vary_on_cookie(func):
  21. """
  22. A view decorator that adds "Cookie" to the Vary header of a response. This
  23. indicates that a page's contents depends on cookies. Usage:
  24. @vary_on_cookie
  25. def index(request):
  26. ...
  27. """
  28. @wraps(func)
  29. def inner_func(*args, **kwargs):
  30. response = func(*args, **kwargs)
  31. patch_vary_headers(response, ('Cookie',))
  32. return response
  33. return inner_func