我在燒瓶中實現(xiàn)了簡單的應(yīng)用程序。我也可以獲取數(shù)據(jù)并處理它,但是如何獲得隨機創(chuàng)建的文件夾。在此應(yīng)用程序中,我嘗試將一些數(shù)據(jù)輸入到文本區(qū)域。單擊“導(dǎo)出甲板”按鈕后,數(shù)據(jù)將發(fā)布到燒瓶中。我也可以獲取數(shù)據(jù)并生成牌組,但無法發(fā)送生成的牌組文件或重定向到隨機文件夾。我收到以下錯誤。raise TypeError(TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.)那么,我該如何實現(xiàn)呢?重定向到新的隨機生成的文件夾或?qū)⑸傻奈募鳛橄螺d鏈接發(fā)送。謝謝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條經(jīng)驗 獲得超4個贊
create_random_folder()
返回重定向,但是當您從home()
請求處理程序調(diào)用它時,您不會對返回值執(zhí)行任何操作,并且不會在處理程序的該代碼分支中返回響應(yīng)home()
。看來您打算從處理程序返回該重定向,home()
如下所示:
return create_random_folder(random_f, data)
請記住,當您從函數(shù)返回值時,您將該值返回給調(diào)用代碼,而不是瀏覽器。如果您從請求處理程序調(diào)用函數(shù)并收到返回值,則該值不會自動發(fā)送回瀏覽器;您需要從請求處理程序返回它。
添加回答
舉報
0/150
提交
取消