首页 > 解决方案 > DJANGO:这个后端不支持绝对路径

问题描述

我正在尝试为必须使用 python open() 方法打开的一些 json 文件设置路径。阅读文档我注意到必须覆盖 django.core.files.storage 中类 Storage 的路径方法,但我不知道如何覆盖它,因为我不知道如何存储此路径。

我的文件是这样的

App
--index_backend
----json
-------some_json.json
----index.py
--views.py
manage.py

我是 Django 的新手,所以如果您需要查看更多代码,请告诉我尽可能多地上传。

更新我想要实现的是在单击按钮时调用一个函数,这个按钮向http://127.0.0.1:8000/App/getValues发送一个请求,以便运行一个 python 函数,该函数调用索引中的另一个 python 函数.py。index.py 里面 some_json.json 是打开的,但是在运行 getValues 时,它会在 /App/getValues/ 处引发 FileNotFoundError,因为它在 /App/getValues/ 中寻找 some_json.json 而不是 /App/index_backend/json/

标签: python-3.xdjangowebdjango-modelsdjango-views

解决方案


如果您使用的是 Django 3.1+,这是一种可以在索引中使用的方法

from django.conf import settings

BASE_DIR = settings.BASE_DIR

JSON_FILE = BASE_DIR / 'App/index_backend/json/some_json.json'

推荐阅读