Known Issues

Problem with some middlewares

Description

Sometimes django_statistics doesn't work well with some middlewares. For example django.middleware.common.CommonMiddleware. It'll sometimes raise the following exception:

Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 277, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 634, in __call__
    return self.application(environ, start_response)
  File "/usr/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 243, in __call__
    response = middleware_method(request, response)
  File "/usr/lib/python2.5/site-packages/django_statistics/middleware.py", line 40, in process_response
    unique = day_obj.add_dataset(request=request)
  File "/usr/lib/python2.5/site-packages/django_statistics/models.py", line 168, in add_dataset
    if request.user.is_authenticated():
AttributeError: 'WSGIRequest' object has no attribute 'user'

This happens for example when you request a 404 URL without a trailing / and CommonMiddleware tries to redirect you to the correct url. The problem is that in the settings.py template for a new project, CommonMiddleware is in the MIDDLEWARE_CLASSES list before SessionMiddleware and AuthenticationMiddleware and if it redirects, it doesn't return None. So any process_request functions of other middlewares which should be executes after CommonMiddleware (like SessionMiddleware and AuthenticationMiddleware) won't be executed and AuthenticationMiddleware doesn't set request.user .

Problem solving

Just move CommonMiddleware below SessionMiddleware and AuthenticationMiddleware in your MIDDLEWARE_CLASSES in your settings.py file.