1

I am trying to set my static file before the base_dir file

like, My root dir is Desktop/afolder/projectdir/settings.py

and my current static file in Desktop/afolder/static/blabla.js

And grab it like with

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

Above those works great, but now i am changing my static file dir, it will be now before the base dir

like: Desktop/static/blabla.js

In this case, i am in trouble to configure my setting so that it get my static files. coz, it is before the base dir, my base dir is afolder like Desktop/afolder/projectdir/settings.py

Can anyone help me to recognize the static file of this dir Desktop/static/blabla.js ?

dudd
  • 372
  • 1
  • 4
  • 13

1 Answers1

1

Use Unipath. It gives you a convenient object-oriented approach to define paths:

from unipath import Path
BASE_DIR = Path(__file__).ancestor(2)

# Another way:
BASE_DIR = Path(__file__).parent.parent

You can install it using pip3 install unipath.

Max Malysh
  • 29,384
  • 19
  • 111
  • 115
  • is not it passable to deal without any third party library? – dudd Aug 24 '19 at 16:26
  • You can fiddle with the built-in `os.path` or `pathlib`, but there is little reason to do so. Unipath is a mature and convenient library. https://stackoverflow.com/questions/2817264/how-to-get-the-parent-dir-location – Max Malysh Aug 24 '19 at 18:11
  • `pathlib` already includes parent(s) methods, it isn't any harder https://docs.python.org/3/library/pathlib.html – Javi Nov 17 '22 at 22:41