Search code examples
web2py

Web2py: How to pass arguments from view to controller to delete a record from DB


I have a table and on each row I have a "Delete" button. What I want to do is pass the row id through onclick function of that button to a "delete" function on controller.

My controller

def studentDelete():
    rec_id = request.args(0)
    crud.delete(db.student, rec_id,next=URL('Home','Students_List'))

My view (when I click on the button, shows a modal to help the user confirm or cancel the delete action. #warning is the id of the accept button from the modal if the user decides to delete the record.)

<div class='btn-group btn-group-justified JpositionA'><a class='btn btn-warning Jview btn-xs opciones'><span class='glyphicon glyphicon-remove'></span></a></div>;

<script>
$(document).on('click', '.opciones', function(e){
      e.preventDefault();
      btn = $(this);
      var tr = $(this).closest('tr');
      var row = tabla.row( tr );
      var datos=row.data();
      var id_record=datos.student.id;

      $('#myModal').modal('show')
     .one('click', '#warning', function(e) {
                     ajax('{{=URL('Tools', 'studentDelete')}}' + '?value=' + id_record);
          });
        });
     });
</script>

Solution

  • The Javascript code is sending the record ID to the server in the query string as the value variable, but the Python code is expecting the ID in request.args. You can simply change the Python code to:

        rec_id = request.get_vars.value