Question

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.

Was it helpful?

Solution

You need getattr().

from django.conf import settings

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

OTHER TIPS

from project import settings
print settings.LABEL_NAME

or

from project.settings import *
print LABEL_NAME
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top