如何迭代Django中的所有设置?

时间:2021-04-10 19:28:08

How can I iterate over all the Django settings? I need to read all the settings which begin with MYAPP_.

如何迭代所有Django设置?我需要阅读以MYAPP_开头的所有设置。

I've tried doing this:

我试过这样做:

from django.conf import settings

for setting in settings:
    print setting

...but i get the following exception:

...但我得到以下异常:

TypeError: 'LazySettings' object is not iterable

Any ideas on how I can accomplish this?

关于如何实现这一目标的任何想法?

3 个解决方案

#1


5  

for s in dir(settings):
    print s, ':', getattr(settings, s)

#2


3  

You should be able to call dir on it.

你应该能够在它上面调用dir。

from django.conf import settings
print dir(settings)

#3


3  

for name in filter(lambda x: x.startswith('MYAPP_'), dir(settings)):
    # ...

#1


5  

for s in dir(settings):
    print s, ':', getattr(settings, s)

#2


3  

You should be able to call dir on it.

你应该能够在它上面调用dir。

from django.conf import settings
print dir(settings)

#3


3  

for name in filter(lambda x: x.startswith('MYAPP_'), dir(settings)):
    # ...