Django + mod_wsgi => “没有模块命名模型”错误

时间:2012-04-09 00:24:53

标签: django apache django-models mod-wsgi wsgi

我需要一些无法解决的Django错误的帮助。

当我使用python manage.py runserver"时,我的网站运行良好,但是当我尝试使用wsgi进行部署时,它会不断出现未知错误。我尽力去解决这些错误,但我无法解决。谁能告诉我的错误是什么?

Apache错误日志

[error] /usr/local/lib/python2.6/dist-packages/django/middleware/csrf.py:262: DeprecationWarning: CsrfResponseMiddleware and CsrfMiddleware are deprecated; use CsrfViewMiddleware and the template tag instead (see CSRF documentation).
[error]   DeprecationWarning
[error] [client 76.191.24.242] mod_wsgi (pid=21082): Exception occurred processing WSGI script '/srv/www/myapp/wsgi.py'.
[error] [client 76.191.24.242] Traceback (most recent call last):
[error] [client 76.191.24.242]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 250, in __call__
[error] [client 76.191.24.242]     self.load_middleware()
[error] [client 76.191.24.242]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 47, in load_middleware
[error] [client 76.191.24.242]     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
[error] [client 76.191.24.242] ImproperlyConfigured: Error importing middleware backends: "No module named models"

wsgi.py

import os
import sys

sys.path.append('/srv/www')
sys.path.append('/srv/www/myapp')

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

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

的Apache

<VirtualHost *:80>

    ServerName www.myapp.com
    DocumentRoot /srv/www/myapp

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.myapp.com$ [NC]
    RewriteRule ^(.*)$ http://www.myapp.com$1 [R,L]

    Alias /static/ /srv/www/myapp/static/

    <Directory /srv/www/myapp>
        Order allow,deny
        Allow from all
    </Directory>

    <Directory /srv/www/myapp/static>
        Order allow,deny
        Allow from all
    </Directory>

    WSGIDaemonProcess myapp.djangoserver processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup myapp.djangoserver
    WSGIScriptAlias / /srv/www/myapp/wsgi.py

</VirtualHost>

1 个答案:

答案 0 :(得分:0)

错误消息显示您使用Django 1.3系列,并且您在当前目录或python中的开发服务器上通过backends.SomeClass(在名为backends的某个模块中定义的某个类)定制了MIDDLEWARE_CLASSES路径,但如果由mod_wsgi启动则不是这样。

应使用中间件类的全名,并验证在生产服务器上添加了必要的python路径。最后在wsgi.py文件中使用sys.path.append。它看起来在开发服务器上你将一些包的目录添加到python路径并且你写了短名称('backends'),而你应该将这个包的父目录添加到sys.path并写入全名。

(当然 - 如果你使用它:如果你使用virtualenv,请验证sys.executable是来自该virtualenv目录的python可执行文件,而不是来自普通的Python。不应该通过添加路径来修复被遗忘的virtualenv。)< / p>