In my Django project I have the following directory structure:
project/build/contracts/MyFile.json
And I am writing code in the following directory
project/homeapp/views.py
In my views.py
I have the following code:
with open("../build/contracts/MyFile.json", "r") as f:
data = json.loads(f.read())
abi = data["abi"]
When I try to python manage.py runserver
I get the following error:
The strange part is that I couldn't figure out what was wrong so I made a viewstest.py
and placed the exact same code in it. When I run it with python .\viewstest.py
and print the JSON to console, it works perfectly fine.
I even tried importing the abi
variable from viewstest.py
to views.py
but got the same error. So I assume that it is an error relating to Django, but I can't seem to figure it out.
Thanks!
CodePudding user response:
It should be json.load() and not json.loads()
Change the following code to:
with open("../build/contracts/MyFile.json", "r") as file:
data = json.load(file)
abi = data["abi"]
Edit:
Another alternative to get the path correct can be to use Pathlib.
from pathlib import Path
def your_view_func(request):
current_dir = Path.cwd()
file_loc = 'build/contracts/MyFile.json'
with open(current_dir.joinpath(file_loc), 'r') as file:
data = json.load(file)
abi = data["abi"]