0

I am new in Django, Here I have added a correct code but it is not deleting the file. Please help me someone.

views.py:

def control_upload(request):  
    if request.method == 'POST':
        form = ControlForm(request.POST, request.FILES)  
        if form.is_valid():
            model_instance = form.save()
            model_instance.save()
    else:
        form = ControlForm()
    controlmachiness = Controlmachine.objects.all()
    return render(request,'usermaster/control_uploadfile.html',{'form':form,'controlmachiness':controlmachiness})

def control_index(request):  
    controlmachines = Controlmachine.objects.all()  
    return render(request,"usermaster/control_show.html",{'controlmachines':controlmachines}) 

def control_destroy(request, id):
    if request.method == 'POST':
        controlmachine = Controlmachine.objects.get(id=id)  
        controlmachine.delete()  
        return HttpResponseRedirect('/usermaster/controlfile/')

models.py:

class Controlmachine(models.Model):  
    machine_name = models.CharField(max_length=100)  
    operation_no = models.IntegerField()  
    control_uploadfile = models.FileField(upload_to='control_files/')
  
    class Meta:  
        db_table = "controlmachine"

    def delete(self, *args, **kwargs):
        self.machine_name.delete()
        self.operation_no.delete()
        self.control_uploadfile.delete()
        super.delete(*args, *kwargs)

forms.py:

class ControlForm(forms.ModelForm):  
    class Meta:  
        model = Controlmachine 
        fields = ['machine_name', 'operation_no', 'control_uploadfile'] #https://docs.djangoproject.com/en/3.0/ref/forms/widgets/
        widgets = { 'machine_name': forms.TextInput(attrs={ 'class': 'form-control' }), 
            'operation_no': forms.TextInput(attrs={ 'class': 'form-control' }),
            'control_uploadfile': forms.ClearableFileInput(attrs={ 'class': 'form-control' }),
      }

urls.py:

path('controlfile/',views.control_index,name='control_index'),
path('cdelete/<int:id>',views.control_destroy,name='control_destroy'),
path('controlupload/',views.control_upload,name='control_upload'),
path('upload/',views.upload,name='upload'),
path('save',views.save_machine,name='save_machine'),

control_show.html:

{% extends 'usermaster/home.html' %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
 
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
</head>
{% block body %}
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="table-responsive">
            <table id="bootstrapdatatable" class="table table-striped table-bordered" width="90%">
                    <thead>
                        <th><input type="checkbox" id="checkall" /></th>
                        <th>ID</th>
                        <th>Machine Name</th>
                        <th>Operation Number</th>
                        <th>File</th>
                        <th>Delete</th>
                     </thead>
            <tbody>
            {% for control in controlmachines %}  
            <tr>  
             <td><input type="checkbox" class="checkthis" /></td>
             <td>{{ control.id }}</td>  
             <td>{{ control.machine_name }}</td>  
             <td>{{ control.operation_no }}</td>  
             <td>{{ control.control_uploadfile }}</td>  
             <td><p data-placement="top" data-toggle="tooltip" title="Delete"><a href="/usermaster/cdelete/{{ control.id }}" class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></a></p></td>  
            </tr>  
            {% endfor %} 
            </tbody>
                 
            </table>
                </div>
            </div>
        </div>
    </div>
    <script>
       $(document).ready(function() {
           $('#bootstrapdatatable').DataTable({     
             "aLengthMenu": [[3, 5, 10, 25, -1], [3, 5, 10, 25, "All"]],
               "iDisplayLength": 3
              } 
           );
       } );
    </script>

</body>
{% endblock %}
</html>

control_uploadfile.html:

{% extends "usermaster/home.html" %}
<html>
<head>
<title>Control File Upload</title>

</head>
{% block body %}
<body>
<p><h1>Control File Upload</h1></p>
<form method="post" enctype="multipart/form-data">  
        {% csrf_token %}  
        {{ form.as_p }}
        <button type="submit" class="save btn btn-default">Save</button> <br><br>
</form>
</body>
{% endblock %} 
</html>

Error I got

ValueError at /usermaster/cdelete/29
The view usermaster.views.control_destroy didn't return an HttpResponse object. It returned None instead.
Request Method: GET
Request URL:    http://localhost:8000/usermaster/cdelete/29
Django Version: 4.0
Exception Type: ValueError
Exception Value:    
The view usermaster.views.control_destroy didn't return an HttpResponse object. It returned None instead.
Exception Location: C:\Users\Manoj\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py, line 309, in check_response
Python Executable:  C:\Users\Manoj\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.5
Python Path:    
['C:\\Users\\Manoj\\Desktop\\sample\\mysite3',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32\\lib',
 'C:\\Users\\Manoj\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\Pythonwin']
Server time:    Mon, 24 Jan 2022 10:15:46 +0530

Here, It should delete uploaded file along with machine name and operation number. Please help me out to solve this. Please..

Manoj Tolagekar
  • 1,816
  • 2
  • 5
  • 22

1 Answers1

0

Looks like you are missing a url on the form.

    action="{% url 'control_destroy' id=id %}"
Ryan Thomas
  • 488
  • 4
  • 14