heri0n
heri0n

Reputation: 1489

Using AJAX in django to delete a row in a table represented by a model

I have a representing a model and I want to be able to remove the row and delete the object from the DB without reloading the page. On the left I have an X icon when clicked hides the row using .hide('slow') but how can I make it remove from the db? Re-loading the page takes a while.. Can I delete the object using a form or using a /delete page?

Upvotes: 0

Views: 1880

Answers (1)

dm03514
dm03514

Reputation: 55932

you can use jquery to easily make an ajax request

as part of the event handler that is triggered by a user clicking 'X', you can make a request to your django app.

$.post('/django/url/to/your/view', {idToDelete: 'value'}, function(response) {
  // callback
});

Of coures this is just on the client side. In django you will need to creat a new urls.py entry and create the view logic to handle actually deleteing the id.

It is very important to include a csrf token in your post requests. Django provides step by step on how to do this with ajax requests. https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax

Upvotes: 1

Related Questions