9

I keep getting this error at random times and whenever I touch the django.wsgi file, it gets fixed only to happen again after a few hours. I'm lost as to what to do. my middleware_classes is as follows:

MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.csrf.CsrfResponseMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.facebookConnectMiddleware.FacebookConnectMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)

The error always occurs in the facebook middleware when i do an "if request.session" statement. Thanks

Tisho
  • 8,320
  • 6
  • 44
  • 52
kingsley
  • 305
  • 3
  • 12

4 Answers4

6

Make sure the SessionMiddleware is first in your MIDDLEWARE_CLASSES.

Jonatan Littke
  • 5,583
  • 3
  • 38
  • 40
  • Starting from Django 2.0 `MIDDLEWARE_CLASSES` is replaced with `MIDDLEWARE` - [more here](https://stackoverflow.com/questions/39480179/wsgirequest-object-has-no-attribute-session-while-upgrading-from-django-1-3) – mateuszb Mar 25 '18 at 23:11
0

Are you using Apache? If so, you should probably restart httpd after you modify the mod_wsgi file.

sudo apachectl -k restart
sudo apache2ctl -k restart
sudo /etc/init.d/httpd restart

... or similar should work. If you're still seeing the problem, try pasting in the full error message.

godswearhats
  • 2,155
  • 1
  • 14
  • 6
  • Whether a full Apache restart is required is dependent on how you are using mod_wsgi. If using daemon mode there are ways of restarting application code without restarting the whole of Apache. See 'http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode'. – Graham Dumpleton Jul 15 '10 at 00:21
  • True enough. I'm diagnosing the symptoms a bit here: typically when I see a problem recur it's because it's fixed in one Apache thread but not in all of them ... hence the restart idea. It's worth a try :-) – godswearhats Jul 15 '10 at 15:36
  • i restarted apache and that seemed to do the trick. thank you both for your input :-) – kingsley Jul 15 '10 at 16:21
  • 1
    nope.. the problem occurred again. entering the url without an ending slash gives the error and it only gets fixed temporarily when i touch django.wsgi..what to do.. – kingsley Jul 18 '10 at 00:25
  • What does your wsgi file look like? and your urls.py? – godswearhats Jul 19 '10 at 14:53
  • I have the same problem in the development server so it's probably not apache related. – Jonatan Littke May 21 '12 at 07:10
0

Try the alternate WSGI script file documented at end of:

http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html

Report back to mod_wsgi list if that helps as we are trying still to uncover what if any problems that alternate WSGI script is solving, so can work out whether a real problem or whether users aren't using Django properly.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
0

Check if in your code you have write:

del request.session

the correctly are is

del request.session['YOU VARIABLE']

Taranttini
  • 1,435
  • 1
  • 11
  • 4