我无法删除图像,但可以使用 Flask 在同一目录中正常工作

I am not able to delete image but insert in working fine with same directory using Flask

我正在尝试从文件夹中删除图像,但无法正常工作并出现错误:FileNotFoundError:[WinError 2] 系统找不到指定的文件:'static\images18194259_a476v_engelhart-tilburg_inside-track_text_font_product.jpg'

但是插入在相同的上传目录下工作得很好

UPLOAD_FOLDER = 'static\images'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

插入代码:

def insert():
cursor = db.cursor()

if request.method == "POST":
    flash("Data Inserted Successfully")
    name = request.form['name']
    email = request.form['email']
    phone = request.form['phone']

    image = request.files['imgfile'] #myfile is name of input tag

    if image and allowed_file(image.filename):
        fileTemp = secure_filename(image.filename)
        time_p = time.strftime('%Y%H%M%S')

        filename = time_p+"_"+fileTemp

        image.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))

        path = filename
    empty=''
    if path is empty:
        return "You have not uploading a image"
    else:
        cursor.execute("INSERT INTO student_flask (name, email, phone)VALUES (%s, %s, %s)", (name, path, phone))
        db.commit()
        cursor.close()

    return redirect(url_for('Index'))

删除图像代码:

@app.route('/delete/<string:id_data>', methods = ['GET'])
def delete(id_data):

   imgname = image_name(id_data)
   mna = imgname[0]
   os.remove(os.path.join(app.config['UPLOAD_FOLDER'], mna))
   return redirect(url_for('Index'))

首先查询数据库以获取图像的名称。如果它本身就是一个对象,即Image.name 或对象属性。即Foo.image。在您的删除功能中,您必须 运行 commit 才能使更改生效。另外 id 应该是 int:

@app.route('/delete/<int:id>', methods=['GET', 'POST'])
def delete(id=None):