Django

Code

Ticket #2541 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

[patch] CacheMiddleware depends on AuthenticationMiddleware is CACHE_ANONYMOUS_ONLY=True

Reported by: dummy@habmalnefrage.de Assigned to: jacob
Milestone: Component: Cache system
Version: magic-removal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

django-middleware-cache.diff (0.8 kB) - added by anonymou on 08/13/06 14:36:05.

Change History

08/13/06 14:36:05 changed by anonymou

  • attachment django-middleware-cache.diff added.

08/17/06 22:31:13 changed by adrian

  • status changed from new to closed.
  • resolution set to fixed.

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

08/17/06 22:33:37 changed by adrian

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


Add/Change #2541 ([patch] CacheMiddleware depends on AuthenticationMiddleware is CACHE_ANONYMOUS_ONLY=True)




Change Properties
Action