简体   繁体   English

django static 文件的相对路径

[英]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.我正在尝试找到一种方法来从 static 文件中读取 JSON 文件,而无需对文件的路径进行硬编码。

   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?有没有办法只用 static 文件夹中的相对路径打开这个文件?

As long as your STATIC_ROOT and STATICFILES_DIR are well configured you can use the finder's module to do that.只要您的 STATIC_ROOT 和 STATICFILES_DIR 配置良好,您就可以使用查找器的模块来执行此操作。 Just make shure you don't have multiple files with the same path after static root.只需确保您在 static 根目录之后没有具有相同路径的多个文件即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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