I have defined constant in django settings file like

LABEL_NAME = 'xyz'

And now I am having string 'LABEL_NAME' with me and want to have its equivalent constant defined value i.e 'xyz'

I can able to access 'xyz' value by LABEL_NAME. But I don't know how it can be accessed by string equivalent to defined constant.

I came from PHP background and worked with codeIgniter framework which provides the ability like this @CONSTANT('LABEL_NAME') to access equivalent defined constant value. Please let me know if we already have such kind of facility available in django/python. I could not get any help even after spending hour searching this problem.

I really appreciate your help of any kind.

有帮助吗?

解决方案

You need getattr().

from django.conf import settings

setting_name = "LABEL_NAME"
print(getattr(settings, setting_name))  

其他提示

from project import settings
print settings.LABEL_NAME

or

from project.settings import *
print LABEL_NAME
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top