djangodjango-settingsdjango-apps

Django settings: How to access variables from the settings folder in an app


I have a Django project with the following structure:

--|src
  --project
  --|settings
     --__init__.py
     --production.py
     --local.py
  --|app1

In my app I import the settings (from django.conf import settings) and then as I was following a tutorial they said to do this getattr(settings, VARIABLE). That doesn't work for me. Instead I can do this: settings.VARIABLE. What's the difference?

Oh and I ran type(settings) and it outputted <class 'django.conf.LazySettings'>.


Solution

  • in order to access variables in settings.py file, you can do like this:

    for example, I define STATIC_ROOT variable in settings.py file like this:

    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static', 'static_root')
    

    and I can access to this variable like this:

    from django.conf import settings
    document_root=settings.STATIC_ROOT