Django Delete Record

Django Delete Record

Deleting Records

To delete a record we do not need a new template, but we need to make some changes to the members template.

Of course, you can chose how you want to add a delete button, but in this example, we will add a “delete” link for each record in a new table column.

The “delete” link will also contain the ID of each record.

Modify Template

Add a “delete” column in the members template:


The result will look like this:


The “delete” link in the HTML table points to so we will add a path() function in the members/ file, that points the url to the right location, with the ID as a parameter:


Code for Deleting Records

Now we need to add a new view called delete in the members/ file:


The delete view does the following:

  • Gets the id as an argument.
  • Uses the id to locate the correct record in the Members table.
  • Deletes that record.
  • Redirects the user back to the index view.

Click on the “delete” link for Jane Doe, and see the result: