I have a json file on Django server
- The file content is dynamically changing
- I don't want to store the json data as Django model in database
My question is: is there a techinically low cost and efficient way to display the json content in Django template as a frontend webpage ? All I need is display the json data in Django app web page
CodePudding user response:
There are some options, the one I recommend is to store your data in a json file inside your project, each time yo need to update your json file, could be through a signal if it is when you save some data, or in the time you need it.
Then in a view, read and throw to render your json file, this way, doesn't require so much processing, or touch the DB, it's like a cache file.
To write it:
with open(
"path_to_file/your_file.json"), "w"
) as f:
json.dump(your_dict_with_data, f)
To render
return HttpResponse(open("path_to_file/your_file.json", 'r'), content_type = 'application/json; charset=utf8')
CodePudding user response:
I feel like I'm missing some information here but any json object/python dictionary can be represented as a string, and there are plenty of ways to format it (For example, json.dumps() with the indent keyword argument). You could just store the string in the context object that's passed to the render function.