我需要拆分 Django 的开发和生产设置。我决定如果设置了 USKOVTASK_PROD
变量,那么应用程序应该使用生产设置。我读了this文章并尝试这样做。
我的片段:
/etc/apache2/sites-enabled/uskovtask.conf:
<VirtualHost *:80>
ServerName uskovtask.*.com
ServerAlias uskovtask.*.com
DocumentRoot /mnt/ebs/uskovtask
Alias /static /mnt/ebs/uskovtask/static/
<Directory /mnt/ebs/uskovtask/static>
Require all granted
</Directory>
#WSGIPythonPath /mnt/ebs/uskovtask
WSGIDaemonProcess uskovtask.*.com python-path=/mnt/ebs/uskovtask:/usr/lib/python2.7/site-packages
WSGIProcessGroup uskovtask.*.com
WSGIScriptAlias / /mnt/ebs/uskovtask/uskovtask/wsgi.py
SetEnv USKOVTASK_PROD 1
<Directory /mnt/ebs/uskovtask/uskovtask>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
wsgi.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "uskovtask.settings")
from django.core.wsgi import get_wsgi_application
_application = get_wsgi_application()
def application(environ, start_response):
if 'USKOVTASK_PROD' in environ:
os.environ.setdefault('USKOVTASK_PROD', environ['USKOVTASK_PROD'])
return _application(environ, start_response)
settings.py的部分:
import os
if 'USKOVTASK_PROD' in os.environ:
from settings_prod import *
else:
from settings_dev import *
但它总是导入 settings_dev 的设置。为什么?
最佳答案
我通过将 wsgi.py 更改为以下内容解决了这个问题:
from django.core.handlers.wsgi import WSGIHandler
import django
import os
class WSGIEnvironment(WSGIHandler):
def __call__(self, environ, start_response):
os.environ['USKOVTASK_PROD'] = environ['USKOVTASK_PROD']
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "uskovtask.settings")
django.setup()
return super(WSGIEnvironment, self).__call__(environ, start_response)
application = WSGIEnvironment()
关于python - Django + mod_wsgi。从 Apache 的 SetEnv 设置操作系统环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38685159/