我在电脑上有一个文件,我试图从django视图中将其作为JSON提供。
def serve(request):
file = os.path.join(BASE_DIR, 'static', 'files', 'apple-app-site-association')
response = HttpResponse(content=file)
response['Content-Type'] = 'application/json'
我得到的是文件路径,当浏览URL时返回。
/Users/myself/Developer/us/www/static/files/apple-app-site-association
这里我做错了什么?
os.path.join
返回一个字符串(路径)。 - Emile Bergeroncontent=file
就可以了。 - qarthandsofile
中,所以content=file
将该字符串作为响应的内容。搜索如何使用Python读取文件。 - Emile Bergeronpython -m SimpleHTTPServer <port>
...虽然有很多限制,但如果你已安装了python,它只需要几秒钟的时间。从Web浏览器中,您将能够导航和下载。 - s g