[英]DJANGO_SETTINGS_MODULE not defined when building Sphinx documentation
我正在学习本教程 ,以便为我的Django项目生成文档。
我已将sys.path.append(os.path.join(os.path.dirname(__name__), '..'))
到文档目录中的conf.py
文件中。
但是运行make html
会一直给我这样的错误:
配置不正确:请求设置LOGGING_CONFIG,但未配置设置。 您必须在访问设置之前定义环境变量DJANGO_SETTINGS_MODULE或调用settings.configure()。
我的项目运行正常,所以我不确定我应该在哪里调用settings.configure()
?
目录结构
基于具有以下目录结构的vanilla安装,这里应该是有效的步骤。 只要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
Sphinx配置
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和解析.rst
文件中的autodoc
指令是必需的。
2⇒设置的环境变量与设置模块的位置。 此示例基于vanilla django-admin设置。 如果您使用更复杂的设置结构(例如dev
, staging
,从某些基本设置扩展的production
设置),只需更新路径,例如myproj.settings.dev
。
3⇒最后调用django.setup()
是必要的填充Django的应用程序的注册表,这狮身人面像,以生成完整的Django设置的文档需要。
现在一切都应该用于$ make html
以使用autodoc
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.