Question

I'm using Django 1.5.1 in virtualenv, Python 2.7 on OS X 10.8.4. I changed from DEBUG = True to DEBUG = False in my Django settings and I got an error:

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/Users/kilroy/.virtualenvs/project_name/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
    response = self.get_response(request)
  File "/Users/kilroy/.virtualenvs/project_name/lib/python2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/Users/kilroy/.virtualenvs/project_name/lib/python2.7/site-packages/django/core/handlers/base.py", line 224, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/Users/kilroy/Sites/PYTHON/project_name/apps/common/views.py", line 436, in handler_500
    messages.error(request, '500 - Internal server error.')
  File "/Users/kilroy/.virtualenvs/project_name/lib/python2.7/site-packages/django/contrib/messages/api.py", line 102, in error
    fail_silently=fail_silently)
  File "/Users/kilroy/.virtualenvs/project_name/lib/python2.7/site-packages/django/contrib/messages/api.py", line 22, in add_message
    raise MessageFailure('You cannot add messages without installing '
MessageFailure: You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware

I'm using different settings file for development and production, and on my development machine I set environment variable in /Users/kilroy/.virtualenvs/project_name/bin/postactivate file like this:

export DJANGO_SETTINGS_MODULE=project_name.settings.local

My base.py file looks like this https://dpaste.de/avXmL/, and my local.py settings like this https://dpaste.de/Z0PXv/.

Why is this happening when I want to disable debugging and how can I trace why this problem occurs in the first place?

Was it helpful?

Solution

I had the same stack trace when I set DEBUG false when using the development server. In my case it was related to the ALLOWED_HOSTS setting / check by Django. The symptoms were confusing because a SuspiciousOperation exception was being raised which caused the error.

If I had ALLOWED_HOSTS = ['localhost'] in my settings file and http://localhost:8000 as my browser address all was well. However, if I use the IP address http://127.0.0.1:8000/ directly I got this error.

OTHER TIPS

Can you try this on shell:

from django.conf import settings
print settings.MIDDLEWARE_CLASSES

And if you don't see django.contrib.messages.middleware.MessageMiddleware in middleware_classes you should add it.

I guess you didn't get any 500 error while in development and so this line using messages.error(request, '500 - Internal server error.') was never executed and so you did not face this problem in DEBUG=True.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top