Get absolute path of django app

后端 未结 6 2083
一整个雨季
一整个雨季 2021-02-03 20:09

I am writing a unit test that needs to access an image file that I put in \"fixtures\" directory right under my django app directory. I want to open up this image file in my tes

6条回答
  •  無奈伤痛
    2021-02-03 21:08

    În newer versions of Django (I don't know when it started, I assume it's there for many years now), the default settingy.py contains an entry

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    

    which you can use by importing the settings file like this

    import os    
    from my_project_name.settings import BASE_DIR
    
    os.path.join(BASE_DIR, ...)
    

提交回复
热议问题