我正在尝试从文件夹中删除图像,但它不起作用,出现错误:文件未发现错误:[WinError 2] 系统找不到指定的文件:"静态\图像\2018194259_a476v_engelhart-tilburg_inside-track_text_font_product.jpg">
但是插入在相同的上传目录中工作得很好
UPLOAD_FOLDER = 'staticimages'
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
或对象属性 .i.即Foo.image
.在删除函数中,必须运行提交才能使更改生效。此外,id
应该是一个int
:
@app.route('/delete/<int:id>', methods=['GET', 'POST'])
def delete(id=None):