我在烧瓶中实现了简单的应用程序。我也可以获取数据并处理它,但是如何获得随机创建的文件夹。在此应用程序中,我尝试将一些数据输入到文本区域。单击“导出甲板”按钮后,数据将发布到烧瓶中。我也可以获取数据并生成牌组,但无法发送生成的牌组文件或重定向到随机文件夹。
我收到以下错误。
raise TypeError(
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.)
那么,我该如何实现呢?重定向到新的随机生成的文件夹或将生成的文件作为下载链接发送。
谢谢
app.py
from flask import Flask, render_template, request, redirect, url_for, flash, send_file, send_from_directory
import image_occ_deck_export
import random, os
app = 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)
繁花不似锦
相关分类