繁体   English   中英

写入json文件时,烧瓶“ ValueError:视图函数未返回响应”

[英]flask “ValueError: View function did not return a response” when write to json file

嗨,大家好,我是我的烧瓶应用程序的观点。 当我将文件上传到我的应用程序时,它会将字典写入指示的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))它也恰好返回了我想要的东西。 但是我无法写入文件,我使用此链接将字典写入json文件。

看来您想做两件事。 您想要将数据写入文件,并且想要在响应中返回该数据。 为此,您需要执行两个单独的步骤。

例如:

@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM