简体   繁体   中英

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""

@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. But I cannot write to file I used this link to write dictionary to the json file.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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