Opened 18 years ago

Closed 18 years ago

Last modified 18 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: no UI/UX: no

Description

Hi,

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

MIDDLEWARE_CLASSES = (

'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',

)

From the tutorial it should be

MIDDLEWARE_CLASSES = (

'django.middleware.cache.CacheMiddleware', # before CommonMiddleware
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',

)

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/basehttp.py", line 272, in run

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

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

return self.application(environ, start_response)

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

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

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

response = middleware_method(request)

File "/usr/local/lib/python2.4/site-packages/django/middleware/cache.py", 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:

MIDDLEWARE_CLASSES = (

'django.contrib.sessions.middleware.SessionMiddleware', ## before AuthenticationMiddleware
'django.contrib.auth.middleware.AuthenticationMiddleware', ## before CacheMiddleware
'django.middleware.cache.CacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.doc.XViewMiddleware',

)

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

Regards,
Dirk

Attachments (1)

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

Download all attachments as: .zip

Change History (3)

by anonymou, 18 years ago

comment:1 by Adrian Holovaty, 18 years ago

Resolution: fixed
Status: newclosed

(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 by Adrian Holovaty, 18 years ago

(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