在django项目中,我具有以下结构。
我有一个provider.py文件,其中:
from django.conf import settings
from project.utils.firewalls import fw
在提供程序中,我需要获取一些在设置中定义的变量。
然后在firewalls.py中,我有:
from django.conf import settings
在firewalls.py中,我确实有一些代码需要获取在设置中定义的变量。
当我尝试做时: python manage.py --settings = project.settings.local它将抱怨没有定义SECRET_KEY,尽管它已定义。
这与我在两个文件中都导入django.conf.settings有关。
如何避免这种情况?
目前,我已经在firewalls.py中对var的值进行了硬编码,但是我想避免这种情况。
编辑:
添加Stacktrack:
➤ python project/manage.py runserver --settings=project.settings.local
Traceback (most recent call last):
File "project/manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 60, in execute
super().execute(*args, **options)
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 67, in handle
if not settings.DEBUG and not settings.ALLOWED_HOSTS:
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 57, in __getattr__
self._setup(name)
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 107, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/home/bsabin/Projects/repo/venv/lib64/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/bsabin/Projects/repo/project/project/settings/local.py", line 1, in <module>
from .base import *
File "/home/bsabin/Projects/repo/project/project/settings/base.py", line 21, in <module>
from project.utils.providers import AProvider, BProvider, CProvider
File "/home/bsabin/Projects/repo/project/project/utils/providers.py", line 6, in <module>
from project.utils.firewalls import fw
File "/home/bsabin/Projects/repo/project/project/utils/firewalls.py", line 29, in <module>
fw = get_firewall_instance()
File "/home/bsabin/Projects/repo/project/project/utils/firewalls.py", line 11, in get_firewall_instance
token = settings.DO_TOKEN.get('token')
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 57, in __getattr__
self._setup(name)
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/home/bsabin/Projects/repo/venv/lib/python3.7/site-packages/django/conf/__init__.py", line 126, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
答案 0 :(得分:1)
我假设您面临的问题是,您正在其他文件中使用函数来提取设置中的某些配置数据,并且在该文件中还导入了设置。在理想情况下,它应该触发循环依赖错误。但是由于django.conf.settings
是lazy,它将引发Improper Configuration Error。
如果需要一些变量进行配置,为什么不将变量直接传递给函数。例如:
# in settings:
from project.utils.providers import AProvider, BProvider, CProvider
SOME_VARIABLE = AProvider(token="Your Token")
然后在提供程序内部
class AProvider(...):
def __init__(self, *args, **kwargs):
token = kwargs.get('token')
# rest of the code
# somewhere in the code
fw = get_firewall_instance(self.token)
在firewall.py
中:
def get_firewall_instance(token_from_settings):
token = token_from_settings