【发布时间】:2017-06-05 13:48:38
【问题描述】:
我正在尝试通过 python 函数写入 json 文件。 python 函数使用 json.dumps 函数将 python 数据结构转储到 json 文件中。 该文件被创建,并且 json 文件中存在 json 对象形式的 python 数据结构,但是当 python 函数中存在的 javascript 函数尝试打开并读取对象时,它会失败。 我收到以下错误:未找到 file_name.json 我很确定该文件确实存在。
def func(request):
arr = [[]]
message = ''
arr[0].append('Hello')
arr[0].append('There')
arr.append([])
arr[1].append('Good')
arr[1].append('Morning')
arr[1].append('!')
message = message + '<html><head><script type="text/javascript" src="outputjson.js"></script><script>function Hello(){alert(hello[1]);}</script></head>'
with open('outputjson.js', 'w') as fo:
jsonp_structure = "hello = "+json.dumps(arr)+" "
fo.write(jsonp_structure)
message = message + '<body><h1>This is shit and you know it</h1><input value="Click Bro" type="button" onclick="Hello();"></input></body></html>'
return HttpResponse(message)
我收到错误消息:未找到:outputjson.js
我是否需要明确告诉 Django 在哪里查找文件?
【问题讨论】:
-
你能检查一下.py文件所在的目录吗?它在那里创建你的文件,就代码而言..它在我的工作正常
-
@Roshan 你好 Roshan,你能打开 .js 文件并使用我制作的 javascript 函数从中读取所需的 json 对象吗?
-
那个 json 文件在哪里?你把它放在静态文件夹中了吗?还是views.py所在的同一个文件夹?
-
with open('outputjson.js', 'r') as fo: fo.read()
-
@Exprator 我尝试将它保存在与views.py 相同的文件夹中,但没有用。然后我在 app 目录下创建了一个单独的“静态”文件夹并运行 python manage.py collectstatic,没有工作。
标签: javascript python json django