下面的朋友是我从烧瓶应用程序中看到的。当我将文件上传到我的应用程序时,它会将字典写入指示的json文件,但作为响应,它会返回错误""ValueError: View function did not return a response""
@app.route('/')
def upload_file_mainpage():
return render_template('index.html')
@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
new_file = request.files['file']
outfile = open('out.json', 'w')
with outfile as outfile:
return json.dump(soupla(new_file), outfile), 200
soupla返回字典我对此没有任何问题,即使我使用json.dumps(soupla(new_file))
,它也会返回我想要的内容。但我无法写入文件,我使用此link将字典写入json文件。
答案 0 :(得分:1)
看起来你想做两件事。您希望将数据写入文件,并且希望在响应中返回该数据。要做到这两点,您需要执行两个单独的步骤。
例如:
@app.route('/')
def upload_file_mainpage():
return render_template('index.html')
@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
new_file = request.files['file']
rv = json.dumps(soupla(new_file))
outfile = open('out.json', 'w')
with outfile as outfile:
outfile.write(rv)
return rv, 200