移动到另一个目录后配置django设置?

时间:2016-02-23 17:54:30

标签: python django django-settings

我的django项目结构如下所示,

.
├── myapp
│   ├── apps
│   │   └── __init__.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
└── tests
    └── test_config.py

从我移动设置文件以形成以下结构。

.
├── myapp
│   ├── apps
│   │   └── __init__.py
│   ├── config
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   └── __init__.py
├── manage.py
└── tests
    └── test_config.py

所以我改变了wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.config.settings")

application = get_wsgi_application()

和我的manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.config.settings")

    print "settings!!"

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

现在当我用./manage.py shell运行django shell时,我收到此错误,下面发布了回溯。

Python 2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010] on linux2
Django 1.9.2
Traceback (most recent call last):
  File "<input>", line 5, in <module>
  File "/home/marty/.virtualenvs/seventeen/local/lib/python2.7/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/marty/.virtualenvs/seventeen/local/lib/python2.7/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/home/marty/.virtualenvs/seventeen/local/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

这里有什么问题?我该如何解决这个问题?

感谢

1 个答案:

答案 0 :(得分:3)

myapp不在PYTHONPATH中,因此myapp.config.settings不是有效的模块。

在os.environ之前,添加以下行:

import sys
sys.path.append('path/to/dir/of/myapp')