Uwsgi:不能将django的设置作为一个包使用

时间:2015-06-28 22:55:26

标签: python django deployment settings uwsgi

我尝试使用套装分割django的设置,在this文章中留下深刻印象。

我的问题是uwsgi不认识我的project.settings包。

[一些歌词] 使用secrets.py我有轻微的修改结构,因此所有文件除了存储在github中的secrets.py和secret.py由ansible管理。

所以我的设置现在是一个包含以下结构的包:

project/
├── manage.py
└── project
    ├── __init__.py
    ├── settings
    │   ├── __init__.py
    │   ├── defaults.py
    │   ├── development.py
    │   ├── production.py
    │   ├── staging.py
    │   └── secrets.py
    ├── urls.py
    └── wsgi.py

重要文件的内容:

设置/ __初始化__。PY

# Appropriate config load based on DJANGO_ENVIRONMENT env 
# variable, which set at system boot

ENVIRONMENT = os.getenv("DJANGO_ENVIRONMENT")

if ENVIRONMENT == "production":
    from production import *
elif ENVIRONMENT == "staging":
    from staging import *
elif ENVIRONMENT == "development":
    from development import *

settings / development.py

from .defaults import *

DEBUG = True

INSTALLED_APPS += (
    'debug_toolbar',
)

设置/ defaults.py

import os
import secrets

SECRET_KEY = secrets.SECRET_KEY

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': secrets.DATABASE_NAME,
        'USER': secrets.DATABASE_USER,
        'PASSWORD': secrets.DATABASE_PASSWORD,
        'HOST': 'localhost',
        'PORT': '',
    }
}

# and so on, almost default

设置/ secrets.py

SECRET_KEY = '1YdCOUsWeKk&BWDa$PI6X1@IdUH5vjkjcB^v&k3J@*fRRVRPrW'

DATABASE_NAME = 'mydb'
DATABASE_USER = 'myuser'
DATABASE_PASSWORD = '1l3S7KURpd1MYPOr'

DJANGO_ENVIRONMENT

$ echo $DJANGO_ENVIRONMENT
development

-------------------------------------------- ----------------------------------------

在这个时候,我可以毫无问题地开始manage runservercheck

$ python manage.py check
System check identified no issues (0 silenced).

但是,如果我尝试使用UWSGI,我在uwsgi-error.log中遇到错误(我的项目名称是pdfupload):

[uwsgi-daemons] respawning "python /home/vagrant/pdfupload/manage.py rqworker default" (uid: 1000 gid: 1000)
Traceback (most recent call last):
  File "/home/vagrant/pdfupload/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 182, in fetch_command
    settings.INSTALLED_APPS
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 113, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

显然,由于导入错误,uwsgi无法读取django的设置。

有uwsgi.ini:

[uwsgi]
chdir=/home/vagrant/pdfupload
master=True
disable-logging=False
chmod-socket = 777
chown-socket = vagrant

logto      = /home/vagrant/log/uwsgi.log
req-logger = file:/home/vagrant/log/uwsgi-access.log
logger     = file:/home/vagrant/log/uwsgi-error.log

vacuum=True
pidfile=/run/uwsgi/pdfupload.pid
max-requests=5000
socket=/run/uwsgi/pdfupload.socket
processes=2
harakiri=20

pythonpath=..
env=DJANGO_SETTINGS_MODULE=pdfupload.settings
module = django.core.wsgi:get_wsgi_application()
touch-reload=/tmp/pdfupload.reload

虚拟环境无法使用。

请帮我完成此设置。

1 个答案:

答案 0 :(得分:0)

您的DJANGO_SETTINGS_MODULE必须指向实际设置文件,因此在您的情况下:

pdfupload.settings.defaults (or whatever in your settings folder)