ここでのエラーは、BadRequestKeyErrorが原因で発生します request.formに存在しないキーへのアクセスのため 。
ipdb> request.form['u_img']
*** BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
アップロードされたファイルは、request.filesの下にキー設定されます request.formではありません 辞書。また、u_imgで入力された値のため、ループを失う必要があります FileStorageのインスタンスです 反復可能ではありません 。
@app.route('/', methods=['GET', 'POST'])
def index():
target = os.path.join(app_root, 'static/img/')
if not os.path.isdir(target):
os.makedirs(target)
if request.method == 'POST':
...
file = request.files['u_img']
file_name = file.filename or ''
destination = '/'.join([target, file_name])
file.save(destination)
...
return render_template('index.html')