Я попробовал сохранить файл через стандартную функцию save и через with open (смотрите ниже)
db_sess = db_session.create_session()
try:
news = db_sess.query(News).get(news_id)
except Exception as ex:
print(ex)
return abort(404)
if request.method == "GET":
return render_template("edit_news.html", news=news)
form = request.form
file = request.files
if "img" in file.keys():
file = file["img"]
filename = file.filename
filetype = filename.rsplit(".", 1)[1]
if file and "." in filename and filetype in ALLOWED_IMAGES_TYPES:
filename = f'{datetime.datetime.now().strftime("%d.%m.%Y;%H:%M:%S")}.{filetype}'
file.save(os.path.join(UPLOAD_FOLDER, secure_filename(filename)))
news.img = filename
news.commit()
Второй вариант (через open)
db_sess = db_session.create_session()
try:
news = db_sess.query(News).get(news_id)
except Exception as ex:
print(ex)
return abort(404)
if request.method == "GET":
return render_template("edit_news.html", news=news)
form = request.form
file = request.files
if "img" in file.keys():
file = file["img"]
filename = file.filename
filetype = filename.rsplit(".", 1)[1]
if file and "." in filename and filetype in ALLOWED_IMAGES_TYPES:
filename = f'{datetime.datetime.now().strftime("%d.%m.%Y;%H:%M:%S")}.{filetype}'
with open(os.path.join(UPLOAD_FOLDER, secure_filename(filename)), "w") as f:
f.write(file.read())
news.img = filename
news.commit()