I have a file on my computer that I'm trying to serve up as JSON from a django view.
file = os.path.join(BASE_DIR, 'static', 'files', 'apple-app-site-association')
response = HttpResponse(content=file)
response['Content-Type'] = 'application/json'
os.path.join returns a string, it's why you get a path in the content of the response. You need to read the file at that path first.
If the file is static and on disk, you could just return it using the webserver and avoid using python and django at all. If the file needs authenticating to be downloaded, you could still handle that with django, and return a
X-Sendfile header (this is dependant on the webserver).
Serving static files is a job for a webserver, Nginx and Apache are really good at this, while Python and Django are tools to handle application logic.
def serve(request): path = os.path.join(BASE_DIR, 'static', 'files', 'apple-app-site-association') with open(path , 'r') as myfile: data=myfile.read() response = HttpResponse(content=data) response['Content-Type'] = 'application/json'
This is inspired by How do I read a text file into a string variable in Python
See dhke's answer on