python - Django WSGI 路径

标签 python django apache apache2 wsgi

我在使用 django 设置 wgsi 时遇到问题。我正在关注这个 http://docs.djangoproject.com/en/dev/howto/deployment/modwsgi/ .然而,我仍然对将 .wsgi 文件放在哪里以及是否需要设置 sys.path 感到困惑。我已经直接在 Web 根目录外部和内部进行了尝试,但我无法按预期进行任何工作。

# /home/ben/public_html/django_test/testproject/apache/django.wsgi:

import os
import sys

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

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

相关的 apache 配置:

DocumentRoot "/home/ben/public_html/django_test/testproject/"
WSGIScriptAlias / "/home/ben/public_html/django_test/testproject/apache/django.wsgi"

Apache 日志错误(标准 Apache 500 页):

ImportError: Could not import settings 'testproject.settings' (Is it on sys.path? ...

我至少可以让 django 抛出一个它自己的错误:

import os
import sys

path = '/home/ben/public_html/django_test/testproject'
if path not in sys.path:
    sys.path.append(path)

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

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

导致此 django 错误页面:

ImportError at /admin/
No module named testproject.urls

最佳答案

我将 wsgi 放在与 settings.py 相同的级别,看起来像这样:

import os
import sys

sys.path.insert(0,os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2]))


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

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

这是 apache 配置文件:

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName www.yourprojectname.com
    Alias /media/ /home/diegueus9/workspace/yourprojectname/media/

    <Directory /home/diegueus9/workspace/yourprojectname/media/>
        Order deny,allow
        Allow from all
    </Directory>

    WSGIScriptReloading On
    WSGIDaemonProcess yourprojectname 
    WSGIProcessGroup yourprojectname
    WSGIApplicationGroup yourprojectname
    WSGIPassAuthorization On

    WSGIScriptAlias / /home/diegueus9/workspace/yourprojectname/yourfile.wsgi
    ErrorLog /var/log/apache2/yourprojectname-error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel debug

    CustomLog /var/log/apache2/yourprojectname-access.log combined

</VirtualHost>

关于python - Django WSGI 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4194243/

相关文章:

python - 正确打印货币符号

python - 从用户定义的环境启动时,为什么 Spyder 5.0.0 应用程序启动错误?

python - 我怎样才能刮掉pdf的几页?

python - Google 应用程序引擎部署 Flask 应用程序 - ImportError : libSM. so.6:无法打开共享对象文件:没有这样的文件或目录

python - 无法在 Django Rest 框架中上传图像并获得预期行为

php - Varnish 503 错误不会消失并且是随机的

java - 无法使用for循环在Excel工作表中写入数据(Apache_poi)

django - 从一条线上处决两名 celery worker

python - 在 Django 中减去日期?

php - Symfony2 在将应用程序上传到共享主机后抛出 ContextErrorException