0

我正在尝试创建一个功能,用户可以在其中即时上传和删除一些图像。删除部分有效但不完全......它成功地从我的静态文件夹中删除了一个图像,但由于某种原因,它只删除了它找到的第一个图像,而不是链接到引导卡的那个。由于某种原因,它没有正确识别正确的值,我不知道为什么:

1. 烧瓶

@app.route('/deleteImg', methods=['GET', 'POST'])
def deleteImg(row):
if request.method == 'POST':
    image_to_del = request.values.get('image_to_del', None)
    print(f'image_to_del:{image_to_del}')
    os.remove(os.path.join(UPLOAD_FOLDER,image_to_del))
   
    return redirect('/uploadimg')
return render_template('uploadimg.html')

2. HTML

<form action="{{url_for('deleteImg')}}" method="POST">
<div class="row">
    {% for row in pics %}
      <div class="col-md-3 mt-3">
        <div class="card">
            <img src="{{ url_for('static', filename='pics/'+row)}}" alt="{{row}}" class="card_img_top" height="200">
            <input type="hidden" name="image_to_del" class="image_to_del" value="{{row}}">Filename : {{row}}</input>
            <h5 class="text-cent" >Filename : {{row}}</h5>
        </div>
        <div class="card-footer text-center">
            <input type="submit" value="Delete" class="btn btn-success" />

        </div>
      </div>
    {% endfor %}
</div>
</form>
4

1 回答 1

0

我为那些可能感兴趣的人找到了这个解决方案。这是基于“Cairocoders” Flask 从数据库中删除图像的工作

1.烧瓶

@app.route('/deletelogo/<string:get_ig>', methods=['GET', 'POST'])
def deletelogo(get_ig):
  
  print(f'get_ig :{get_ig}')
  os.remove(os.path.join(LOGO_FOLDER,get_ig))
  return redirect('/logoimg')

2. HTML

    <div class="row">
    {% for row in pics %}
      <div class="col-md-3 mt-3">
        <div class="card">
            <img src="{{ url_for('static', filename='logo/'+row)}}" alt="{{row}}" class="card_img_top" height="200">
            <input type="hidden" name="image_to_del" class="image_to_del" value="{{row}}">Filename : {{row}}</input>
            <h5 class="text-cent" >Filename : {{row}}</h5>
        </div>
        <div class="card-footer text-center">
            <td><p><a href="/deletelogo/{{row}}" class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-trash"></span></a></p></td>
    </div>
 </div>
于 2021-08-25T08:27:04.447 回答