MaNKuR
MaNKuR

Reputation: 2704

django/python: to access defined constant by equivelent string

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.

Upvotes: 0

Views: 117

Answers (2)

RemcoGerlich
RemcoGerlich

Reputation: 31270

You need getattr().

from django.conf import settings

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

Upvotes: 1

user1301404
user1301404

Reputation:

from project import settings
print settings.LABEL_NAME

or

from project.settings import *
print LABEL_NAME

Upvotes: 0

Related Questions