The Create, Read and Update operations are performed on the Notes Model. Now its time to delete a note from the database.
A particular instance of a note can be deleted from the database using the delete method of the object. A function can be written in the notesapp\views.py
file for the same as follows :
def note_delete(request, pk):
note = Note.objects.get(pk=pk)
note.delete()
return redirect('home')
This function gets the instance using its primary key and then calls the delete
method to remove it from the database.
A route for the above function can be created in the notesapp\urls.py
file as follows :
path('note-delete/<int:pk>', views.note_delete, name='note_delete'),
And the corresponding link, should be updated in the home.html
file as well.
<a href="{% url 'note_delete' note.pk %}" class="btn btn-danger">Delete</a>
Now, clicking on the delete button would remove that particular note from the database.
The first note, with title of Meeting Agenda has been removed from the database, by clicking the delete button.