Lỗi xảy ra do BadRequestKeyError vì quyền truy cập vào một khóa không tồn tại trong request.form .
ipdb> request.form['u_img']
*** BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
Các tệp đã tải lên được khóa trong request.files chứ không phải request.form từ điển. Ngoài ra, bạn cần mất vòng lặp vì giá trị được khóa dưới u_img là một bản sao của FileStorage và không có thể lặp lại .
@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')