Ad

I'm Having Trouble With My Django Code While I Deleting The Data From The Table Form On Click Of The Delete Button

- 1 answer

But it will showing an error which is (TypeError at /deletecase deletecase() missing 1 required positional argument: 'pk') please help me out this

Here is the url link of the page:

```path("deletecase",views.deletecase,name="deletecase")```

THE VIEW FILE FUNCTION:

```
def policcasesdetails(request):
    cases=Case.objects.all()
    return render(request,'policeside/policcasesdetails.html',{'cases':cases})

def deletecase(request,pk):
    obj=Case.objects.get(id=pk)
    obj.delete()
    return HttpResponse("Data is Deleted")
```

AND THE FORM IS:-

    <div class="container">
       <div class="content-middle">
          <div class="col-md-12 sed-in">
             <h3>Cases Details</h3>
             {% for i in cases %}
             <div class="col-md-4 sed-top">
                <div class="top-sed">
                   <!-- <img src="{{ i.image.url }}" class="img-responsive" alt=""> -->
                   <!-- <label><span>{{ i.oname }}</span></label> -->
                </div>
                <h4>
                   Case Number- <a>{{i.casenumber}}</a>
                </h4>
                <p>Case Name-  <a target="_blank" rel="nofollow noreferrer" href="#">{{i.casename}}</a></p>
                <p>Case Result- <a target="_blank" rel="nofollow noreferrer" href="">{{ i.caseresult }}</a></p>
                <p>Case Conclusion- <a target="_blank" rel="nofollow noreferrer" href="">{{ i.casecoclusion }}</a></p>
                <p><a target="_blank" rel="nofollow noreferrer" href="{% url 'deletecase' i.id %}">
                   <button type="submit" value="delete" class="btn btn- 
                      danger">Delete</button>
                </p>
             </div>
             {% endfor %}
             <div class="clearfix"> </div>
          </div>
          <div class="clearfix"> </div>
       </div>
    </div>

Ad

Answer

In your URL, pk is missing.

path("deletecase/<id>/",views.deletecase,name="deletecase")

This should solve your issue

Ad
source: stackoverflow.com
Ad