Django Redherring Error with Cache Middleware and Anonymous Only

You will sometimes see an error related to your middleware ordering when you have CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True but I have found that sometimes the error has nothing to do with the ordering but is caused by another application or module that is loaded by your main application.

We are using the Satchmo eCommerce module in one of our Django applications, which works great and provides a solid ecommerce solution, among other things. When we migrated from python2.4 to python2.5, I had to build python2.5 from source and build and install all of the dependent packages from source as well, as we are using CentOS 5 and Red Hat Enterprise 5, which currently ships with python2.4. When I had finished that labourious process, I fired up our staging server to see how our application would fare in this new environment. I did not expect everything to work straight away, but I certainly did not expect the error that presented itself to me upon loading the home page in Firefox:

AssertionError: The Django cache middleware with
CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True
requires authentication middleware to be installed.
Edit your MIDDLEWARE_CLASSES setting to insert 
'django.contrib.auth.middleware.AuthenticationMiddleware'
before the CacheMiddleware.

I had seen this error before and thought that I had resolved the problem by some secret voodoo magic in the ordering of the middleware packages in my settings file. Malcolm Tredinnick, one of the core Django developers, mentioned this problem in the Django Users group back in December, and validated my theory about ordering and secret incantations. But here was the error again, so my middleware ordering must be off, I thought. I was using python2.5 on my Ubuntu laptop, so I did not think it had anything to do with python2.5. I figured that something was not working right with the new python 2.5 packages. I commented out the anonymous only line in my settings file and kill HUP'd Apache.

Lo and behold, there was another error that did not surface with the anonymous only cache setting equal to True. Satchmo uses the PyCrypto package for blowfish encryption in its payment module and it seems that I had failed to install PyCrypto, even though I had downloaded the tarball and unpacked it for installation. Here is the error:

File "/xxx/satchmo/payment/models.py", line 7, in 
from Crypto.Cipher import Blowfish
ImportError: No module named Crypto.Cipher

After building PyCrytpo and installing it, I uncommented the anonymous only line for my cache configuration in my settings file, kill HUP'd apache and everything worked as it should.

1 Comment, 0 trackbacks (Trackback URL)

1 response to Django Redherring Error with Cache Middleware and Anonymous Only

  1. Alexander Ljungberg  May 7th, 2009 at 12:34 p.m.

    Great post. We had a real head-scratcher moment this morning about this error when setting up some new servers. Turns out the problem was just the permissions on a logging folder.

Leave a Comment
  1. (required)
  2. Ignore this field:
  3. Don't put anything in this field:
    Don't put anything here:
  4. Leave this empty:
    (required)
  5. Your email is not publically displayed.