How to set STATIC_ROOT and MEDIA_ROOT so that the path used is not hard-coded?

In my settings.py file, both tags STATIC_ROOTand MEDIA_ROOTboth point to a hard coded location. For example, the path STATIC_ROOT:

/home/ian/projectname/mysite/appname/static

I know that this will cause problems when deploying my project.

Looking around, I see what I need to use os.path, but countless examples just confused me.

I tried to look at the different permutations for installing this file (or get the example value used to set BASE_DIR), however, as shown on the screen, I missed something because it complains about the file value.

If necessary, I use Django 1.6

Thanks in advance.

enter image description here

+4
2

-, NameError: name '__file__' is not defined . __file__, filepath, .

, , os.path.join(os.path.dirname(__file__)) file.py, python file.py . , __file__ , filepath of file.py.

settings.py BASE_DIR = os.path.dirname(os.path.dirname(__file__)), Django. os.path.join() , :

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
TEMPLATE_DIRS = os.path.join(BASE_DIR, 'templates')

, Django, . Django?.

+4

( django) BASE_DIR, __file__:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

, os.path.join(), , :

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

, __file__ , .

+3

All Articles