Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#2541 closed defect (fixed)

[patch] CacheMiddleware depends on AuthenticationMiddleware is CACHE_ANONYMOUS_ONLY=True

Reported by: dummy@… Owned by: jacob
Component: Core (Cache system) Version: magic-removal
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:



if you set CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True the default order in MIDDLEWARE_CLASSES won't work




From the tutorial it should be


'django.middleware.cache.CacheMiddleware', # before CommonMiddleware


but with this order and CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True you get the following error:

Traceback (most recent call last):

File "/usr/local/lib/python2.4/site-packages/django/core/servers/", line 272, in run

self.result = application(self.environ, self.start_response)

File "/usr/local/lib/python2.4/site-packages/django/core/servers/", line 611, in call

return self.application(environ, start_response)

File "/usr/local/lib/python2.4/site-packages/django/core/handlers/", line 148, in call

response = self.get_response(request.path, request)

File "/usr/local/lib/python2.4/site-packages/django/core/handlers/", line 59, in get_response

response = middleware_method(request)

File "/usr/local/lib/python2.4/site-packages/django/middleware/", line 51, in process_request

if self.cache_anonymous_only and request.user.is_authenticated():

AttributeError: 'WSGIRequest' object has no attribute 'user'

The correct order I asume is:


'django.contrib.sessions.middleware.SessionMiddleware', ## before AuthenticationMiddleware
'django.contrib.auth.middleware.AuthenticationMiddleware', ## before CacheMiddleware


To give the user a better error message for this dependancy I attach a fix.


Attachments (1)

django-middleware-cache.diff (804 bytes) - added by anonymou 9 years ago.

Download all attachments as: .zip

Change History (3)

Changed 9 years ago by anonymou

comment:1 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

(In [3603]) Fixed #2541 -- Added helpful error message for CacheMiddleware in the case of CACHE_ANONYMOUS_ONLY=True and uninstalled/unordered AuthenticationMiddleware. Thanks, dummy@…

comment:2 Changed 9 years ago by adrian

(In [3604]) Added note to docs/cache.txt about CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True and ordering AuthenticationMiddleware. Refs #2541.

Note: See TracTickets for help on using tickets.
Back to Top