Forums

Mezzanine install Unhandled exception

I am trying to get my Mezzanine install working on PA. But I get the unhandled exception.

I have moved a working install from my Ubuntu machine to PA and modified the WSGI file to match PA.

I am using the virtualenv guidance, and has ensured right versions of all modules.

I have also been able to run python manage.py runserver in the project directory.

The error log says:

2014-02-26 08:14:10,933 :Traceback (most recent call last):
2014-02-26 08:14:10,934 :  File "/bin/user_wsgi_wrapper.py", line 67, in __call__
2014-02-26 08:14:10,934 :    self.error_log_file.logger.exception("Error running WSGI application")
2014-02-26 08:14:10,934 :  File "/usr/lib/python2.7/logging/__init__.py", line 1183, in exception
2014-02-26 08:14:10,935 :    self.error(msg, *args, **kwargs)
2014-02-26 08:14:10,935 :  File "/usr/lib/python2.7/logging/__init__.py", line 1176, in error
2014-02-26 08:14:10,935 :    self._log(ERROR, msg, args, **kwargs)
2014-02-26 08:14:10,935 :  File "/usr/lib/python2.7/logging/__init__.py", line 1268, in _log
2014-02-26 08:14:10,936 :    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2014-02-26 08:14:10,936 :  File "/usr/lib/python2.7/logging/__init__.py", line 1242, in makeRecord
2014-02-26 08:14:10,936 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2014-02-26 08:14:10,936 :  File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2014-02-26 08:14:10,936 :    self.threadName = threading.current_thread().name
2014-02-26 08:14:10,936 :  File "/usr/lib/python2.7/threading.py", line 1158, in currentThread
2014-02-26 08:14:10,937 :    return _active[_get_ident()]
2014-02-26 08:14:10,937 :  File "/bin/user_wsgi_wrapper.py", line 59, in __call__
2014-02-26 08:14:10,937 :    app_iterator = self.app(environ, start_response)
2014-02-26 08:14:10,937 :  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 250, in __call__
2014-02-26 08:14:10,937 :    self.load_middleware()
2014-02-26 08:14:10,937 :  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 51, in load_middleware
2014-02-26 08:14:10,938 :    raise exceptions.ImproperlyConfigured('Middleware module "%s" does not define a "%s" class' % (mw_module, mw_classname))
2014-02-26 08:14:10,938 :django.core.exceptions.ImproperlyConfigured: Middleware module "mezzanine.core.middleware" does not define a "RedirectFallbackMiddleware" class

[edited by admin: formatting]

Hello,

You don't use "python manage.py runserver" to get your server working. Instead you need to use WSGI to point at your application, and then you click "Reload xxx"

Thanks for replying...

I am aware that I should use the web app stuff, but doing the runserver can confirm to me that it seems that things are working.

I just did a pip freeze on both hosts and the lists are 100% similar.

I recommend using python manage.py shell for testing instead of runserver. It gives you a command line where you can import and check stuff more easily.

Anyway, that doesn't fix your problem... It looks to me like there's a version clash going on. Do you get exactly the same version of mezzanine from your pip freeze in both environments?

Checked again, exactly same version both places.

(test1)08:40 ~/proj3 $ python manage.py shell                                                                                                                     
/home/njbuch/.virtualenvs/test1/local/lib/python2.7/site-packages/mezzanine/utils/conf.py:51: UserWarning: You haven't defined the ALLOWED_HOSTS settings, which Django 1.5 requires. Will fall back to the domains configured as sites.
warn("You haven't defined the ALLOWED_HOSTS settings, which "
Python 2.7.5+ (default, Sep 19 2013, 13:48:49)     
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>

Which version of Mezzanine is it?

(test1)08:16 ~/proj3 $ pip freeze
Django==1.6.2
Mezzanine==3.0.9
MySQL-python==1.2.5
Pillow==2.3.0
argparse==1.2.1 
bleach==1.4
filebrowser-safe==0.3.2
future==0.9.0
grappelli-safe==0.3.6
html5lib==1.0b3
oauthlib==0.6.1
pytz==2013.9
requests==2.2.1
requests-oauthlib==0.4.0
six==1.5.2
tzlocal==1.0
wsgiref==0.1.2

What happens if you do a

from mezzanine.core.middleware import RedirectFallbackMiddleware

...from that manage.py shell?

nothing....or I mean, it returns no errors...

Argh! Sorry, I should have spotted this earlier. You're in a virtualenv -- are you activating it in your WSGI file? If not, that would explain everything we're seeing. Search for activate_this on the virtualenv help page for details.

Well spotted, had that earlier, disappeared on a retry. Fixed now. But still unhandled exception:

2014-02-26 13:09:37,555 :Traceback (most recent call last):
2014-02-26 13:09:37,555 :  File "/bin/user_wsgi_wrapper.py", line 67, in __call__
2014-02-26 13:09:37,555 :    self.error_log_file.logger.exception("Error running WSGI application")
2014-02-26 13:09:37,556 :  File "/usr/lib/python2.7/logging/__init__.py", line 1183, in exception
2014-02-26 13:09:37,556 :    self.error(msg, *args, **kwargs)
2014-02-26 13:09:37,556 :  File "/usr/lib/python2.7/logging/__init__.py", line 1176, in error
2014-02-26 13:09:37,556 :    self._log(ERROR, msg, args, **kwargs)
2014-02-26 13:09:37,557 :  File "/usr/lib/python2.7/logging/__init__.py", line 1268, in _log
2014-02-26 13:09:37,557 :    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2014-02-26 13:09:37,557 :  File "/usr/lib/python2.7/logging/__init__.py", line 1242, in makeRecord
2014-02-26 13:09:37,557 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2014-02-26 13:09:37,558 :  File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2014-02-26 13:09:37,558 :    self.threadName = threading.current_thread().name
2014-02-26 13:09:37,558 :  File "/usr/lib/python2.7/threading.py", line 1158, in currentThread
2014-02-26 13:09:37,558 :    return _active[_get_ident()]
2014-02-26 13:09:37,558 :  File "/bin/user_wsgi_wrapper.py", line 59, in __call__
2014-02-26 13:09:37,558 :    app_iterator = self.app(environ, start_response)
2014-02-26 13:09:37,559 :  File "/home/njbuch/.virtualenvs/test1/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__
2014-02-26 13:09:37,559 :    self.load_middleware()
2014-02-26 13:09:37,559 :  File "/home/njbuch/.virtualenvs/test1/lib/python2.7/site-packages/django/core/handlers/base.py", line 49, in load_middleware
2014-02-26 13:09:37,560 :    mw_instance = mw_class()
2014-02-26 13:09:37,560 :  File "/home/njbuch/.virtualenvs/test1/lib/python2.7/site-packages/django/middleware/locale.py", line 24, in __init__
2014-02-26 13:09:37,561 :    for url_pattern in get_resolver(None).url_patterns:
2014-02-26 13:09:37,561 :  File "/home/njbuch/.virtualenvs/test1/lib/python2.7/site-packages/django/core/urlresolvers.py", line 346, in url_patterns
2014-02-26 13:09:37,562 :    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
2014-02-26 13:09:37,562 :  File "/home/njbuch/.virtualenvs/test1/lib/python2.7/site-packages/django/core/urlresolvers.py", line 341, in urlconf_module
2014-02-26 13:09:37,562 :    self._urlconf_module = import_module(self.urlconf_name)
2014-02-26 13:09:37,563 :  File "/home/njbuch/.virtualenvs/test1/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
2014-02-26 13:09:37,563 :    __import__(name)
2014-02-26 13:09:37,563 :ImportError: No module named proj3.urls

Aha, progress :-)

That looks like a path issue. In your WSGI file, you'll have some kind of code to add the directory containing the project code to the path. If your URLs are specified as proj3.urls, this directory should be the one that contains the directory proj3. My guess is that you've added the proj3 directory itself (it's a common mistake).

If that's not the case, can I look at your files and see if there's something more obscure going on?

ok, I have tried to change stuff in the file, but does not work, here is file:

# This file contains the WSGI configuration required to serve up your
# web application at http://njbuch.pythonanywhere.com/
activate_this = '/home/njbuch/.virtualenvs/test1/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import os
import sys

# django settings file is at '/home/njbuch/proj3/settings.py'
path = '/home/njbuch/'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'proj3.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

And its giving me these errors:

2014-02-26 13:37:32,585 :Traceback (most recent call last):
2014-02-26 13:37:32,585 :  File "/bin/user_wsgi_wrapper.py", line 67, in __call__
2014-02-26 13:37:32,585 :    self.error_log_file.logger.exception("Error running WSGI application")
2014-02-26 13:37:32,586 :  File "/usr/lib/python2.7/logging/__init__.py", line 1183, in exception
2014-02-26 13:37:32,586 :    self.error(msg, *args, **kwargs)
2014-02-26 13:37:32,586 :  File "/usr/lib/python2.7/logging/__init__.py", line 1176, in error
2014-02-26 13:37:32,586 :    self._log(ERROR, msg, args, **kwargs)
2014-02-26 13:37:32,587 :  File "/usr/lib/python2.7/logging/__init__.py", line 1268, in _log
2014-02-26 13:37:32,587 :    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2014-02-26 13:37:32,587 :  File "/usr/lib/python2.7/logging/__init__.py", line 1242, in makeRecord
2014-02-26 13:37:32,587 :    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2014-02-26 13:37:32,588 :  File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2014-02-26 13:37:32,588 :    self.threadName = threading.current_thread().name
2014-02-26 13:37:32,588 :  File "/usr/lib/python2.7/threading.py", line 1158, in currentThread
2014-02-26 13:37:32,588 :    return _active[_get_ident()]
2014-02-26 13:37:32,588 :  File "/bin/user_wsgi_wrapper.py", line 59, in __call__
2014-02-26 13:37:32,588 :    app_iterator = self.app(environ, start_response)
2014-02-26 13:37:32,589 :  File "/home/njbuch/.virtualenvs/test1/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in __call__
2014-02-26 13:37:32,589 :    self.load_middleware()
2014-02-26 13:37:32,589 :  File "/home/njbuch/.virtualenvs/test1/lib/python2.7/site-packages/django/core/handlers/base.py", line 46, in load_middleware
2014-02-26 13:37:32,590 :    for middleware_path in settings.MIDDLEWARE_CLASSES:
2014-02-26 13:37:32,590 :  File "/home/njbuch/.virtualenvs/test1/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
2014-02-26 13:37:32,591 :    self._setup(name)
2014-02-26 13:37:32,591 :  File "/home/njbuch/.virtualenvs/test1/lib/python2.7/site-packages/django/conf/__init__.py", line 49, in _setup
2014-02-26 13:37:32,592 :    self._wrapped = Settings(settings_module)
2014-02-26 13:37:32,592 :  File "/home/njbuch/.virtualenvs/test1/lib/python2.7/site-packages/django/conf/__init__.py", line 151, in __init__
2014-02-26 13:37:32,592 :    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
2014-02-26 13:37:32,593 :django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Would you mind if I took a look at your files? That's probably going to be the fastest way to debug this.

No, go ahead its a clean mezzanine-project, with no edits.

OK, that would explain it. Django requires a unique SECRET_KEY value to be set in settings.py. I think the fabric-based deployment system they have sets one up for you, but it looks like the way you've created it hasn't.

So you need to add a line to /home/njbuch/proj3/settings.py setting SECRET_KEY to a unique value. There's a web app to generate one here.

Hmm, it seems that the local_settings.py is not read by the script, very strange as it works with same versions on my internal server.

this code is not working:

try:
    from local_settings import *
except ImportError, e:
    print 'Unable to load local_settings.py:', e

and throws the exception...

OK, that makes sense. Because you've adjusted your sys.path to have the directory that contains proj3 instead of proj3 itself, then now you need to do:

try:
    from proj3.local_settings import *
except ImportError, e:
    print 'Unable to load local_settings.py:', e

...to make it consistent.

Alternatively, if you want to be able to import things with or without the proj3 at the start depending on context, you could add both directories to sys.path in your WSGI file.

The reason it works in your dev server (I suspect) is that when you run it there, you're using manage.py runserver and so you're running it from the directory that contains settings.py. The current working directory is on the sys.path, and so that means that you can import local_settings without the extra proj3.

It was an uphill battle, now it works really well. Thanks so much Giles.

Let me just recommend a better mezzanine tutorial for python anywhere. The static url stuff also took me some time.

Obviously the biggest difference is runserver missing, the static files, and the virtualenv requirement.

Fantastic, we made it! Glad to hear it's working.

Agreed re: a Mezzanine tutorial. I'll get it on the list.