简体   繁体   English

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

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

hi guys below is my view from my flask application. 嗨,大家好,我是我的烧瓶应用程序的观点。 When I am uploading file to my application it writes dictionary to json file which was indicated but in response it returns error that ""ValueError: View function did not return a response"" 当我将文件上传到我的应用程序时,它会将字典写入指示的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 returns dictionary I have no problem with that and even when I use json.dumps(soupla(new_file)) it returns exactly what I want. soupla返回字典,我对此没有任何问题,即使当我使用json.dumps(soupla(new_file))它也恰好返回了我想要的东西。 But I cannot write to file I used this link to write dictionary to the json file. 但是我无法写入文件,我使用此链接将字典写入json文件。

It looks like you want to do two things. 看来您想做两件事。 You want to write the data to a file, and you want to return that data in the response. 您想要将数据写入文件,并且想要在响应中返回该数据。 To do both, you need to do two separate steps. 为此,您需要执行两个单独的步骤。

For example: 例如:

@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