0

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.

MaNKuR
  • 2,578
  • 1
  • 19
  • 31

2 Answers2

1

You need getattr().

from django.conf import settings

setting_name = "LABEL_NAME"
print(getattr(settings, setting_name))  
RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
0
from project import settings
print settings.LABEL_NAME

or

from project.settings import *
print LABEL_NAME
  • How I can able to access it if I have `'LABEL_NAME'` instead of `LABEL_NAME`. Does this make sense :) – MaNKuR Nov 05 '13 at 14:48
  • You can't, 'LABEL_NAME' is a string. Why do you want to do that? Post your case in detail. –  Nov 05 '13 at 15:01
  • can't we have any method to convert string to equivalent global defined constant and fetch its value. I dont know if django/python have this feature available. But if it dont then I would like to know for what reason. Is this something that not be addressed or paid attention before. Or may be it just a stupid and foolish thing to do the way I want :). – MaNKuR Nov 05 '13 at 15:08
  • 1
    You can do this `eval('LABEL_NAME')` and it will output the value of `LABEL_NAME`. In your case, there's no need to use `eval`. –  Nov 05 '13 at 15:17
  • Thanks @void. this helped :). But would you mind to explain how we can have it without using `eval` ? – MaNKuR Nov 05 '13 at 15:22