Code

Opened 8 years ago

Closed 8 years ago

Last modified 8 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:

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 8 years ago.

Download all attachments as: .zip

Change History (3)

Changed 8 years ago by anonymou

comment:1 Changed 8 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 8 years ago by adrian

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.