cms ImproperlyConfigured: Error importing middleware cms.middleware.media

I'm moving an application that uses django_cms from one server, where everything worked, to another and have spent the last 4 hours trying to find the cause of this error. A suggestions very welcome!

 mod_wsgi (pid=21972): Exception occurred within WSGI script '/var/www/vhosts/compdoctest.com/django/compdoc/django.wsgi'.
 Traceback (most recent call last):
   File "/usr/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 230, in __call__
     self.load_middleware()
   File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 42, in load_middleware
     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
 ImproperlyConfigured: Error importing middleware cms.middleware.media: "No module named media"

The offending line is the last one in the middleware list in settings.py

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'cms.middleware.page.CurrentPageMiddleware',
    'cms.middleware.user.CurrentUserMiddleware',
    'cms.middleware.multilingual.MultilingualURLMiddleware',
    'cms.middleware.media.PlaceholderMediaMiddleware',
    )

If I remove the final line then the code continues and falls over later saying that item is required in middleware.

I'm using slightly different version of django, 1.2.1 final on the old working server and 1.2.3 final on the new server.

All the things I've tried:

  • The same version of django_cms - 2.1.0 beta 3 - that was used on the old server
  • The latest version on github - 2.1.0.beta2-550 Putting the cms, mptt, menus, publisher folders in the
  • app From python importing the
  • googled (nobody having the same problem that I can find)
  • middleware file directly (no problem)
  • result of opening in python:

    python manage.py shell        
    Python 2.5.2 (r252:60911, Jan 20 2010, 23:14:04) 
    [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> import cms.middleware.media
    >>> cms.middleware.media.__file__
    '/var/www/vhosts/compdoctest.com/django/compdoc/cms/middleware/media.pyc'
    >>> from django.forms.widgets import Media
    >>> import cms.middleware.media
    >>> 
    

    Thanks to my friend Bartosz Ptaszynski for pointing me in the right direction on this one. It was a path problem. Added this to the top of the settings.py file and it all magically started working.

    sys.path.insert(0, '/path_to_app/app/')

    And as he pointed out:

    Exception occurred within WSGI script means that the path while running under the web server was wrong it's a completely different environment than the manage.py shell


    I had the same problem. But since it only occurs when using mod_wsgi, another solution is adding the path in the apache config (rather than editing the syspath inside setting.py):

        # mod_wsgi settings
    WSGIDaemonProcess name user=user group=group python-path=/app_path/app/:/app_path/lib/python2.6/site-packages/:/app_path/
    WSGIProcessGroup polykum
    

    Including the site packages in the path is also given in the example of Jon Black.

    链接地址: http://www.djcxy.com/p/48590.html

    上一篇: Ninject 2.1 ActivationException:激活字符串时出错

    下一篇: cms错误配置:导入中间件cms.middleware.media时出错