Django: how to pass a table id in a button to delete view

Viewed 128

So some context:

I have for example a specific car with it's description on : http://localhost:8000/vehicle/1 (note the pk of the vehicle here.) On this page you can apart from changing data and so on you can also add all types of documents and there is a table showing your uploaded documents:

Document table HTML

<h4>Overview Documents</h4>
       <div class="row">
          <div class="col">
           <div class="table-responsive">
            <table class="table table-striped table-sm">
              <thead>
                <tr>
                  <th>#</th>
                  <th>PDF Name</th>
                  <th></th>
                  <th></th>

                </tr>
              </thead>
              <tbody>
                {% for item in all_docs %}   
                  <tr>                
                    <td>{{ forloop.counter }}</td>
                    <td> {{ item.pdf.namepdf|default_if_none:'' }} </td>
                    <td><button class="btn btn-sm btn-secondary" type="button" name="delete_vehdoc">Delete</button>
                        
                    </td>

This is my views.PY

def updatevehicle(request, pk):

...


all_docs = DOC_PDF_Vehicles.objects.filter(vehicle=pk, user=request.user)
context = {"all_docs": all_docs}

if request.method == "POST" and 'delete_vehdoc' in request.POST:
    vehdoc = DOC_PDF_Vehicles.objects.get(id=id, user=request.user)     
    vehdoc.delete()
return redirect('update_vehicle', pk=pk)

Now the issue is as you can see here. I use a lot of ID's and PK's

In my delete view you can see i use an id to retrieve the correct db record but I have atm no idea how to give that ID from my template to the view.

Please also note that I basically refresh the original page (which also has an pk) to update the table view on the front end.

So i need in the end both vehicle pk to redirect and that table record id to delete.

Thanks in advance for the ideas!

0 Answers
Related