简体   繁体   中英

Relative path to django static file

I am trying to find a way to read a JSON file from static files without hardcoding the path to the file.

   def value_setup():
       with open('path/to/static/config/species.json') as f:
           data = json.loads(f.read())

       values = data["value"]
       return values

Is there a way to open this file with just a relative path from the static folder?

As long as your STATIC_ROOT and STATICFILES_DIR are well configured you can use the finder's module to do that. Just make shure you don't have multiple files with the same path after static root.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM