Документация: https://flask-ptbr.readthedocs.io/en/latest/patterns/fileuploads.html
Я следовал приведенной выше документации, чтобы сохранить загрузки фотографий из моего приложения flask; Тем не менее, я получаю KeyError в UPLOAD_FOLDER.
Я сохранил путь UPLOAD_FOLDER в файле flask .cfg в папке экземпляра:
# flask.cfg
TOP_LEVEL_DIR = os.path.abspath(os.curdir)
UPLOAD_FOLDER = TOP_LEVEL_DIR + '/project/static/img/'
здесь - route.py
from flask import render_template, Blueprint, request, redirect, url_for, flash
from project.models import Plants
from .forms import AddPlantForm, EditPlantForm
from project import db, app
from werkzeug.utils import secure_filename
import os
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@plants_blueprint.route('/new', methods=['GET','POST'])
def add_plant():
form = AddPlantForm()
if request.method == 'POST':
if form.validate_on_submit():
if 'plant_photo' not in request.files:
flash('No plant photo added')
return redirect(request.url)
file = request.files['plant_photo']
if file.filename=='':
flash('No plant photo selected ')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
url = os.path.join(app.config['IMAGE_URL'], filename)
else:
filename = ''
url = ''
KeyError в строке ниже
#KeyError: 'UPLOAD_FOLDER'
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))