[Answer]-How to store json data in static file in Django view, and call this function from another function ?

1👍

✅

view is just a Python function that takes an HttpRequest as its first parameter and returns an instance of HttpResponse

so def getjson(request): is just a function, not view, because doesn’t return instance of HttpResponse and yes you can call it (function) from another function or view

You can dump your data to specified location, using smth like this:

import json, os
from django.core.urlresolvers import resolve
from your_project_dir.settings import BASE_DIR

local_file_path = '{0}/{1}/{2}'.format(resolve(request.path).app_name, 'statics', 'd.json')
global_file_path = os.path.join(BASE_DIR, local_file_path)

with open(global_file_path, 'w') as outfile:
   json.dump(data, outfile)

Leave a comment