如何从第三方模块访问Django设置?

2024-04-18 19:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个django不可知模块,它将被导入django子目录树中的任何地方。你知道吗

如果这个模块是从任意django项目中导入的,它会检测到它在django环境中运行, 它会找到并导入设置.py,从项目内部的任何地方?你知道吗

例如。 \django公司 \应用程序 \导入三十、 设置? 你知道吗

\django公司 \项目 \某处 \导入三十、 设置你知道吗

(我正在寻找一个通用的解决方案,而不是特定的dir。)


Tags: 模块项目djangopy应用程序地方dir公司
1条回答
网友
1楼 · 发布于 2024-04-18 19:29:17

您的模块应该以标准方式访问django设置:

from django.conf import settings

https://docs.djangoproject.com/en/1.8/topics/settings/#using-settings-in-python-code

Also note that your code should not import from either global_settings or your own settings file. django.conf.settings abstracts the concepts of default settings and site-specific settings; it presents a single interface. It also decouples the code that uses settings from the location of your settings.

如果模块有自己的设置,可以在项目的设置.py. 您可以在模块中硬编码合理的默认值,如果需要设置.py价值观。你知道吗

相关问题 更多 >