python – 首先启动Django项目错误

我的电脑运行Ubuntu 12.04,我按照本教程开始使用Django:
http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/

我尝试通过运行此命令启动/ srv / www / my alex项目sudo django-admin.py startproject alex

然后创建了apache文件夹和django.wsgi文件(/srv/www/alex/apache/django.wsgi)

树/ / srv / www / alex /

.
├── apache
│   └── django.wsgi
├── alex
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

2 directories, 6 files

alex apache网站:

<VirtualHost *:80>

    ServerName alex.djangoserver
    DocumentRoot /srv/www/alex

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

    WSGIDaemonProcess alex.djangoserver processes=2 threads=15 display-name=%{GROUP}
    WSGIProcessGroup alex.djangoserver

    WSGIScriptAlias / /srv/www/alex/apache/django.wsgi

</VirtualHost>

apache错误日志结果:

[error] [client 127.0.0.1] mod_wsgi (pid=28076): Exception occurred processing WSGI script '/srv/www/floqq/alex/django.wsgi'.
[error] [client 127.0.0.1] Traceback (most recent call last):
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
[error] [client 127.0.0.1]     self.load_middleware()
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
[error] [client 127.0.0.1]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
[error] [client 127.0.0.1]     self._setup()
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
[error] [client 127.0.0.1]     self._wrapped = Settings(settings_module)
[error] [client 127.0.0.1]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 95, in __init__
[error] [client 127.0.0.1]     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[error] [client 127.0.0.1] ImportError: Could not import settings 'alex.settings' (Is it on sys.path?): No module named alex.settings

UPDATE

阿帕奇/ django.wsgi

import os
import sys

path = '/srv/www'
if path not in sys.path:
    sys.path.insert(0, '/srv/www')

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

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

最佳答案 在您的wsgi文件中,您有:

sys.path.insert(0, '/srv/www')

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

这将搜索/ srv / www / alex /中的settings.py.

这失败了,因为根据您发布的树,settings.py位于/ srv / www / alex / alex /中.

所以你有3个选择:

>更改插入的路径
>更改目录结构,或
> refactor / srv / www / alex /到包含alex.settings的包中.

点赞