Reputation: 12471
I am using django 3
I want to get root directory of project.
I googled around and found that I should use this
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
in settings.py though,
I can't figure out how to use SITE_ROOT
in other script.
Maybe this is quite silly and newbee question though,, ーーーーーーーーーーーーーー
thanks to @neverwalkaloner
It works.
from django.conf import settings
print(settings.SITE_ROOT)
but it does not work on apache with wsgi.py. show /
only.
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.myconf.local')
application = get_wsgi_application()
manage.py
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.myconf.local')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
Upvotes: 2
Views: 4867
Reputation: 368
You should import your settings module and use SITE_ROOT as below wherever you need it in your app as below :
from django.conf import settings
...
settings.SITE_ROOT
Upvotes: 2
Reputation: 47374
You can access django settings using django.conf
module. In any django file you can do something like this:
from django.conf import settings
print(settings.SITE_ROOT)
From django docs:
Also note that your code should not import from either global_settings or your own settings file. django.conf.settings abstracts the concepts of default settings and site-specific settings; it presents a single interface. It also decouples the code that uses settings from the location of your settings.
Upvotes: 5