Django:更改项目文件夹/设置结构

时间:2018-08-08 15:19:03

标签: django

我目前正在尝试从默认的Django结构更改为该结构。

enter image description here

我现在复制了新文件夹中的所有文件,但是运行python manage.py runserver --settings=settings.local时,它将在我的终端上显示以下内容:

Traceback (most recent call last):
  File "manage.py", line 16, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/core/management/__init__.py", line 317, in execute
    settings.INSTALLED_APPS
  File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/site-packages/django/conf/__init__.py", line 106, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Users/Marc/.local/share/virtualenvs/lumis-vJ5Odiz7/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'settings'

有人可以看到我在做什么错吗? 我还将wsgi.py和manage.py更改为以下内容:

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

enter image description here

3 个答案:

答案 0 :(得分:0)

您需要通过创建空白的config/文件将config/__init__.py目录转换为python模块,然后在DJANGO_SETTINGS_MODULE和{{1}中像这样配置manage.py }:

config/wsgi.py

然后,您应该可以只运行os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local") 而无需使用./manage.py runserver选项。

答案 1 :(得分:0)

您需要告诉django在哪里寻找设置模块,因此必须在manage.py中指定模块路径:

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

并在设置文件夹内的__init__.py中添加以下行:

try:
    from config.settings.local import *
except:
    pass

答案 2 :(得分:0)

如果您使用的是Pycharm,请转到 文件>设置>语言和框架> Django 并配置

1)[checked]  Django support 
2) settings: config\settings.py
3)manage script: manage.py

enter image description here

但是,如果您使用的是独立控制台,则只需使用文件夹config中的___init__.py文件和os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")文件中的配置the manage.py,就可以了。 >

python manage.py runserver