簡體   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