50

I'm getting the project path in my Django app in settings.py using:

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

I would like to use the PROJECT_PATH value in other views, such as for locating the path to files in my static path. How can I make this into an accessible variable?

Geuis
  • 41,122
  • 56
  • 157
  • 219

3 Answers3

64

Use from django.conf import settings but mind that settings is not a module. The documentation clearly explains that and your use case.

AndiDog
  • 68,631
  • 21
  • 159
  • 205
30
from django.conf import settings as conf_settings
project_path = conf_settings.PROJECT_PATH
yuv
  • 499
  • 1
  • 7
  • 14
Muhammad Taqi
  • 5,356
  • 7
  • 36
  • 61
4
  1. in settings.py add

    DOMAIN = "example.com"
    
  2. views.py

    from django.conf import settings
    
    DOMAIN = settings.DOMAIN
    
  3. lets try to output it:

    print(DOMAIN)
    
    print(type(DOMAIN))
    
  4. output will be:

    example.com
    
    <class 'str'>
    
oruchkin
  • 1,145
  • 1
  • 10
  • 21