在可重复使用的django应用程序中导入设置文件

时间:2022-10-29 13:56:34

I am creating a reusable django application which can be used in any django project, and storing some credentials in the settings.py file.

我正在创建一个可重用的django应用程序,可以在任何django项目中使用,并在settings.py文件中存储一些凭据。

settings.py

# Django settings for mytestapp project.
...
...
...
ADMIN_USERNAME='someone'
ADMIN_PASSWORD='someone'

I want to use these credentials in my app. How can I import them ?

我想在我的应用程序中使用这些凭据。我该如何导入它们?

I have tried,

我试过了,

import settings.ADMIN_USERNAME

and

from django.conf import settings.ADMIN_USERNAME

But both are not working.

但两者都不起作用。

I tried to import these variables from eclipse using ctrl+1 , then these variables imported as,

我尝试使用ctrl + 1从eclipse导入这些变量,然后将这些变量导入为,

from mytestapp.settings import ADMIN_USERNAME

How can I import these variables, if I don't know project name (in case mytestapp) ?

如果我不知道项目名称(如果是mytestapp),我该​​如何导入这些变量?

2 个解决方案

#1


from django.conf import settings

来自django.conf导入设置

Then you can access your constants like settings.ADMIN_USERNAME.

然后,您可以访问settings.ADMIN_USERNAME等常量。

#2


Read the documentation. Don't try to import a setting by name; you should just import the settings:

阅读文档。不要尝试按名称导入设置;你应该只导入设置:

from django.conf import settings

and refer to your specific settings as attributes:

并将您的特定设置称为属性:

settings.ADMIN_USERNAME

#1


from django.conf import settings

来自django.conf导入设置

Then you can access your constants like settings.ADMIN_USERNAME.

然后,您可以访问settings.ADMIN_USERNAME等常量。

#2


Read the documentation. Don't try to import a setting by name; you should just import the settings:

阅读文档。不要尝试按名称导入设置;你应该只导入设置:

from django.conf import settings

and refer to your specific settings as attributes:

并将您的特定设置称为属性:

settings.ADMIN_USERNAME