Repository: django-cms/django-cms

Issue:
ImportError when using wsgi
With cms 3.0b4.dev6 when wsgi handler tries to load middlewares in `django/core/handlers/wsgi.py, line 236 in __call__: self.load_middleware()` then `cms.middleware.page.CurrentPageMiddleware` fails with:

```
ImproperlyConfigured: Error importing middleware cms.middleware.page: "cannot import name clear_app_resolvers"
```



Discussion:
@darbula could you post the whole traceback? python manage.py shell
import cms.middleware.page

I could not reproduce the error from the shell, this the traceback left in apache error.log:

```
mod_wsgi (pid=11466): Exception occurred processing WSGI script '/home/user/project/py/djangoproject/wsgi.py'.
Traceback (most recent call last):
  File "/home/user/project_env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/home/user/project_env/lib/python2.7/site-packages/django/core/handlers/base.py", line 53, in load_middleware
    raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware cms.middleware.page: "cannot import name clear_app_resolvers"
```

In order to get full traceback in file `/home/user/project_env/src/django-cms/cms/signals/apphook.py` there is only occurence of `import clear_app_resolvers` so I have replaced

```
from cms.appresolver import clear_app_resolvers
```

with

```
try:
    from cms.appresolver import clear_app_resolvers
except Exception as e:
    raise Exception('Test: %s' % e)
```

and got this:

```
mod_wsgi (pid=12773): Exception occurred processing WSGI script '/home/user/project/py/djangoproject/wsgi.py'.
Traceback (most recent call last):
  File "/home/user/project_env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/home/user/project_env/lib/python2.7/site-packages/django/core/handlers/base.py", line 51, in load_middleware
    mod = import_module(mw_module)
  File "/home/user/project_env/lib/python2.7/site-packages/django/utils/importlib.py", line 38, in import_module
    __import__(name)
  File "/home/user/project_env/src/django-cms/cms/middleware/page.py", line 2, in <module>
    from cms.appresolver import applications_page_check
  File "/home/user/project_env/src/django-cms/cms/appresolver.py", line 10, in <module>
    from cms.models.pagemodel import Page
  File "/home/user/project_env/src/django-cms/cms/models/__init__.py", line 18, in <module>
    from cms import signals as s_import
  File "/home/user/project_env/src/django-cms/cms/signals/__init__.py", line 2, in <module>
    from cms.signals.apphook import debug_server_restart
  File "/home/user/project_env/src/django-cms/cms/signals/apphook.py", line 6, in <module>
    raise Exception('Test: %s' % e)
Exception: Test: cannot import name clear_app_resolvers
```


Resolving patch:
diff --git a/cms/signals/apphook.py b/cms/signals/apphook.py
--- a/cms/signals/apphook.py
+++ b/cms/signals/apphook.py
@@ -1,6 +1,5 @@
 # -*- coding: utf-8 -*-
 import sys
-from cms.appresolver import clear_app_resolvers
 from django.core.management import color_style
 from django.core.urlresolvers import clear_url_caches
 from django.core.signals import request_finished
@@ -79,6 +78,7 @@ def trigger_restart(**kwargs):
 
 
 def debug_server_restart(**kwargs):
+    from cms.appresolver import clear_app_resolvers
     if 'runserver' in sys.argv or 'server' in sys.argv:
         clear_app_resolvers()
         clear_url_caches()


Test patch: