django和mod_wsgi的配置问题

时间:2010-04-06 18:18:01

标签: python django apache mod-wsgi

我在使用mod_wsgi让django在apache 2.2上工作时遇到了问题。 Django也安装了mod_wsgi。 访问路径时我甚至可以看到404页面,我可以登录django admin。 但是如果我想安装标记模块,我会收到以下错误:

 Traceback (most recent call last):
   File "setup.py", line 49, in <module>
  version_tuple = __import__('tagging').VERSION
   File "/home/jim/django-tagging/tagging/__init__.py", line 3, in <module>
  from tagging.managers import ModelTaggedItemManager, TagDescriptor
   File "/home/jim/django-tagging/tagging/managers.py", line 5, in <module>
  from django.contrib.contenttypes.models import ContentType
   File "/usr/lib/python2.5/site-packages/django/contrib/contenttypes/models.py", line 1, in <module>
  from django.db import models
   File "/usr/lib/python2.5/site-packages/django/db/__init__.py", line 10, in <module>
  if not settings.DATABASE_ENGINE:
   File "/usr/lib/python2.5/site-packages/django/utils/functional.py", line 269, in __getattr__
  self._setup()
   File "/usr/lib/python2.5/site-packages/django/conf/__init__.py", line 40, in _setup
  self._wrapped = Settings(settings_module)
   File "/usr/lib/python2.5/site-packages/django/conf/__init__.py", line 75, in __init__
  raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
 ImportError: Could not import settings 'mysite.settings' (Is it on sys.path? Does it have syntax errors?): No module named mysite.settings

我的httpd.conf:

 Alias /media/ /home/jim/django/mysite/media/

 <Directory /home/jim/django/mysite/media>
  Order deny,allow
  Allow from all
 </Directory>

 Alias /admin/media/ "/usr/lib/python2.5/site-packages/django/contrib/admin/media/"

 <Directory "/usr/lib/python2.5/site-packages/django/contrib/admin/media/">
  Order allow,deny
  Allow from all
 </Directory>

 WSGIScriptAlias /dj /home/jim/django/mysite/apache/django.wsgi

 <Directory /home/jim/django/mysite/apache>
  Order deny,allow
  Allow from all
 </Directory>

我的django.wsgi:

import sys, os

sys.path.append('/home/jim/django')
sys.path.append('/home/jim/django/mysite')

os.chdir('/home/jim/django/mysite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

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

我试着让它在几天后开始工作,并在这里阅读了几篇博客和答案,但没有任何效果。

修改

现在我用this blog post尝试了,我的wsgi文件现在看起来像这样:

import sys
sys.path.insert(0, '/home/jim/django/mysite')
sys.path.insert(0, '/home/jim/django')

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()

admin仍然有效,但是当我尝试安装标记模块时,我遇到了同样的错误。

2 个答案:

答案 0 :(得分:4)

你读过Graham Dumpleton的blog post on Django and WSGI吗?它可以很好地描述一些常见的配置问题,特别是触及mysite.settingssettings的对比。

更新:请阅读Graham Dumpleton在下面的优秀评论。

更新2:Graham和BecomeGuru都指出,问题根本不在于WSGI。这是您安装django-tagging的问题。接受成为Guru的建议,并使用pip安装django-tagging

答案 1 :(得分:4)

首先,

  • 由于您的管理员工作,使用wsgi的设置很好。不要打扰更改/编辑它。

要确保它不是Apache / mod-wsgi设置问题,您可以从生产计算机运行开发服务器

python manage.py runserver 0:8080

然后将浏览器指向

http://yoursite.com:8080/

您必须看到完全相同的行为。

然后,

用于调试此问题:

  • 在服务器上的python shell上,尝试import tagging。很明显,从你的追溯中,import tagging是引发错误的地方,这就是为什么,无法导入设置。

  • 然后,只需删除包含标记的包,然后通过以下命令进行全新安装,该命令知道如何安装包。

sudo pip install django-tagging