我在燒瓶中實現了簡單的應用程序。我也可以獲取數據并處理它,但是如何獲得隨機創建的文件夾。在此應用程序中,我嘗試將一些數據輸入到文本區域。單擊“導出甲板”按鈕后,數據將發布到燒瓶中。我也可以獲取數據并生成牌組,但無法發送生成的牌組文件或重定向到隨機文件夾。我收到以下錯誤。raise TypeError(TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.)那么,我該如何實現呢?重定向到新的隨機生成的文件夾或將生成的文件作為下載鏈接發送。謝謝app.pyfrom flask import Flask, render_template, request, redirect, url_for, flash, send_file, send_from_directoryimport image_occ_deck_exportimport random, osapp = Flask(__name__)app.config["CACHE_TYPE"] = "null"@app.route("/", methods=["GET","POST"])def home(): if request.method == "POST": print(request.form['notes']) notes = request.form['notes'] # random folder data = request.form['notes'] print(data) random_f = random.randrange(1 << 30, 1 << 31) create_random_folder(random_f, data) else: return render_template("index.html") def create_random_folder(random_f, data): directory = str(random_f) parent_dir = "static/uploads/" path = os.path.join(parent_dir, directory) if not os.path.exists(path): os.mkdir(path) random_file = directory + ".txt" random_deck_name = directory + ".apkg" file_loc = path + "/" + random_file deck_loc = path + "/" + random_deck_name with open(file_loc, 'w') as f: f.write(str(data)) image_occ_deck_export.exportDeck(file_loc, deck_loc) return redirect(url_for('uploaded', path=path))@app.route('/uploaded/<path>', methods=['GET'])def uploaded(): return render_template("upload.html")if __name__ == "__main__": app.run(debug=False)
1 回答

繁花不似錦
TA貢獻1851條經驗 獲得超4個贊
create_random_folder()
返回重定向,但是當您從home()
請求處理程序調用它時,您不會對返回值執行任何操作,并且不會在處理程序的該代碼分支中返回響應home()
??磥砟蛩銖奶幚沓绦蚍祷卦撝囟ㄏ?,home()
如下所示:
return create_random_folder(random_f, data)
請記住,當您從函數返回值時,您將該值返回給調用代碼,而不是瀏覽器。如果您從請求處理程序調用函數并收到返回值,則該值不會自動發送回瀏覽器;您需要從請求處理程序返回它。
添加回答
舉報
0/150
提交
取消