Entry only delete first object

Hello Every time i delete my object entry with pop up button. My first entry is deleted and when i deleted it without using pop up it will delete the entry which i select.

views.py:-
def category_delete(request, id):
if request.method == ‘POST’:
pi=Category.objects.get(pk=id)
pi.delete()
return HttpResponseRedirect(’/api/custom_admin/category’)

category.html

                                                            <!-- Modal Header -->
                                                            <div class="modal-header">
                                                                <h4 class="modal-title">Click Yes for Delete Product</h4>
                                                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                                                            </div>

                                                            <!-- Modal body -->
                                                            <div class="modal-body">
                                                                <div class="mod-main">
                                                                
                                                                    <form action="/api/custom_admin/deletecategory/{{cat.uuid}}" method="post">{% csrf_token %}
                                                                    <div class="appendid"></div><input type="submit" class="btn btn-danger" value="Yes"></form>
                                                                    
                                                                    

                                                                    


                                                                
                                                                    <button class="btn btn-warning" data-dismiss="modal"> No </button>
                                                                </div>
                                                            </div>

This looks like django so my reply is based on django.

One thing missing is what your urls.py file looks like.

You can review html forms here, you’re missing a name attribute:

https://www.w3schools.com/html/html_forms.asp

You don’t need to (and probably shouldn’t on a post request) pass the id like this. Give your form inputs name attributes like:
name=“I’d”.

Then in your view access id with:
request.POST[“id”]

Adjust the path in you urls.py accordingly.

urls.py

path('category/', views.show_category, name='showcategory'),
path('addcategory/', views.add_category, name='addcategory'),
path('updatecategory/<str:id>', views.category_edit, name='updatecategory'),
path('deletecategory/<str:id>', views.category_delete, name='deletecategory'),

That is what I expected would be there, which is fine for a GET request. But for a POST request look at my previous comment.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.