title: Django: Manage.py runserver no error, but mod_wsgi report cannot import name connection
body: <p>When testing my django website by <code>python manage.py runserver 0.0.0.0:8000</code>, there is no error.</p>
  
  <p>But if I deploy it to production with apache and mod_wsgi, it reports error <code>cannot import name connection</code>.</p>
  
  <p>I find some other questions like <a href="https://stackoverflow.com/questions/11711536/django-1-4-database-router-cannot-import-name-connection">django 1.4 database router - &quot;cannot import name connection&quot;</a> suggesting adding <code>from django.db import connections</code> into settings.py.<br/></p>
  
  <p>But I found adding this import can prevent this error, but it also disables database router.</p>
  
  <p>I think it may due to different running environment between <code>manage.py runserver</code> and WSGI.
  This is how my wsgi.py looks like</p>
  
  <pre><code>import os
  import sys
  
  sys.path.insert(0,"/home/my/myweb")
  os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mywebsite.settings")
  
  from django.core.wsgi import get_wsgi_application
  
  application = get_wsgi_application()
  </code></pre>
  
  <p>How can I fix this bug?</p>
  
  <p>My python version is 2.7 and django version is 1.4.3</p>
  
  <p><b>Edit 1:</b></p>
  
  <p>My OS is CentOS 6.4, on this server:</p>
  
  <p>If I run it using <code>python manage.py runserver 0.0.0.0:8000</code>, no error.</p>
  
  <p>If I remove database router, it can work under WSGI.</p>
  
  <p>But database router and WSGI cannot work together.</p>
  
  <p>Any advice is appreciated.</p>
  
  <p><b>Edit 2:</b></p>
  
  <p>Thanks to @Graham Dumpleton, I figured it out by myself.</p>
  
  <p>This is the wsgy.py that works for me.</p>
  
  <pre><code>import sys
  
  sys.path.insert(0, '/home/my/myweb')
  
  from mywebsite import settings
  
  import django.core.management
  django.core.management.setup_environ(settings)
  utility = django.core.management.ManagementUtility()
  command = utility.fetch_command('runserver')
  
  command.validate()
  
  import django.conf
  import django.utils
  
  django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)
  
  import django.core.handlers.wsgi
  
  application = django.core.handlers.wsgi.WSGIHandler()
  </code></pre>