Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to delete a file on click of delete button from the html table and move that file from pending to delete folder

What I have tried:

views.py
def pending(request):
    id=request.GET.get('id')
    a=fileupload.objects.all().order_by('id')
    mem=fileupload.objects.get(id=id)
    mem.delete()
    mem.close()
    pending = r"C:/Minorproject/minorproject/uploads/pending/"
    delete = r"C:/Minorproject/minorproject/uploads/delete/"
    for file_name in os.listdir(pending):
        source = pending + file_name
        destination = delete + file_name
        if os.path.isfile(source):
            shutil.move(source, destination)
            print('Moved:', file_name)
    mydict={"data":a}
    return render(request, 'college/pending.html',mydict)


models.py
class fileupload(models.Model):
    branch=models.CharField(max_length=100)
    batch=models.IntegerField()
    uploaddt=models.DateField(null=True)
    uploadby=models.CharField(max_length=100)
    status=models.CharField(max_length=100,default="pending")
    ufile=models.FileField(upload_to='uploads/pending/', null=True)


pending.html
{% extends 'base.html' %}
{% load static %}
{% block a %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Pending Files</title>
</head>
<body>
<div class="row" style="min-height:500pxd">
    <div class="col-sm-2"></div>
    <div class="col-sm-8 pt-5">
<table class="table table-striped table-hover">

    <caption class="table caption-top">Pending table</caption>
    <thead>
  <tr>
      <th scope="col">ID</th>
      <th scope="col">File name</th>
      <th scope="col">File Download</th>
      <th scope="col">File Delete</th>
      <th scope="col">File Process</th>
    </tr>
  </thead>
  <tbody>
    {% for a in data %}
    <tr>
      <th scope="row">{{a.id}}</th>
      <td>{{a.branch}}_{{a.batch}}.csv</td>
      <td><a href="/{{a.ufile}}"><button class="color border border-0" >class="fa-solid fa-download fs-4"></button></a></td>
      <td><a href="delete/{{a.id}}"><button class="color border border-0" >^__i class="fa-solid fa-delete fs-4"></button></a></td>

    </tr>
    {% endfor %}
    </tbody>
</table>
    </div>
    <div class="col-sm-2"></div>
</div>
</body>
{% endblock %}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900