2016-03-25 57 views
1

我正在關注this tutorial,以便爲我的Django項目生成文檔。構建Sphinx文檔時未定義DJANGO_SETTINGS_MODULE

我已將sys.path.append(os.path.join(os.path.dirname(__name__), '..'))添加到我的文檔目錄中的conf.py文件中。

但運行make html一直給我這樣的錯誤:

ImproperlyConfigured:要求設置LOGGING_CONFIG,但沒有配置設置。在訪問設置之前,您必須定義環境變量DJANGO_SETTINGS_MODULE或調用settings.configure()。

我的項目運行良好,所以我不知道我應該在哪裏撥打settings.configure()

回答

3

目錄結構

此基礎上香草在這裏下面的目錄結構安裝是應該的工作步驟。只要conf.py中的路徑設置正確,目錄結構可能會不同。

├── docs 
│   ├── Makefile 
│   ├── build 
│   └── source 
│    ├── _static 
│    ├── _templates 
│    ├── conf.py 
│    └── index.rst 
└── myproj 
    ├── anapp 
    │   ├── __init__.py 
    │   ├── admin.py 
    │   ├── apps.py 
    │   ├── migrations 
    │   │   └── __init__.py 
    │   ├── models.py 
    │   ├── tests.py 
    │   └── views.py 
    ├── manage.py 
    └── myproj 
     ├── __init__.py 
     ├── settings.py 
     ├── urls.py 
     └── wsgi.py 

獅身人面像配置

conf.py需要一些設置,以便autodoc能夠引導Django的應用程序:

# If extensions (or modules to document with autodoc) are in another directory, 
# add these directories to sys.path here. If the directory is relative to the 
# documentation root, use os.path.abspath to make it absolute, like shown here. 
sys.path.insert(0, os.path.join(os.path.abspath('.'), '../../myproj')) # <•• 1 

os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'     # <•• 2 

import django 
django.setup()               # <•• 3 

1⇒追加Django項目目錄(一與manage.py)到Python路徑,這是必要的2和解決autodoc di在.rst文件中提到。

2⇒使用設置模塊的位置設置env變量。這個例子是基於一個香草django管理設置。如果您使用更復雜的設置結構(例如,從某些基本設置擴展的dev,staging,production設置),則只需更新路徑(例如, myproj.settings.dev

3⇒最後調用django.setup()需要填充Django的應用程序的註冊表,這獅身人面像,以生成完整的Django設置的文檔需要。

一切都應該現在的$ make htmlautodoc一起工作。

+0

Ahh ...對我的'conf.py'進行了這些更改,並將我的文檔文件夾移至myproject之外,現在它可以正常工作!謝謝! – thanksd