#2541 closed defect (fixed)
[patch] CacheMiddleware depends on AuthenticationMiddleware is CACHE_ANONYMOUS_ONLY=True
Reported by: | 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)
Change History (3)
by , 18 years ago
Attachment: | django-middleware-cache.diff added |
---|
comment:1 by , 18 years ago
Resolution: | → fixed |
---|---|
Status: | new → 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@…