python-3.xdjangodjango-modelsdjango-views

DJANGO: This backend doesn't support absolute paths


I´m trying to set a path for some json files I have to open with python open() method. Reading documentation I noticed that path method from class Storage in django.core.files.storage must be overrided but I don't know how to override it since I don't know how to store this path.

My files are this way

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

I'm quite new on Django, so if you need to see some more code, please tell me to upload as much as needed.

Update What I want to achieve is to call a function when a button is clickled, this button sends a request to http://127.0.0.1:8000/App/getValues in order to run a python function which calls another python function in index.py. Inside index.py some_json.json is opened, but when running getValues, it raises FileNotFoundError at /App/getValues/, because its looking for some_json.json in /App/getValues/ instead of /App/index_backend/json/


Solution

  • None of above worked, after a lot of searching, this worked for me.

    First, added MEDIA ROOT because I needed these JSON files to be dynamic

    MEDIA_ROOT  = os.path.join(BASE_DIR, 'App/media')
    MEDIA_URL = '/media/'
    

    Then, on index_backend/index.py imported default_storage

    from django.core.files.storage import default_storage
    with default_storage.open('some_json.json', "r") as some_json_file:
          doSomethingJSONGLYCool(some_json_file)
    

    It is important to set media folder as

    App
    --index_backend
    ----index.py
    --media
    ---path/to/json
    --views.py
    manage.py