Hello guys, welcome to LearnVern.
Last topic we saw how we updated data of users.
And after updating how we showed the data in show.html.
Today we will see how to delete data in Django.
Right?
So here, whenever you talk about delete, we will use Delete method.
Okay?
This is an ORM Method, which I told you about when we did Introduction to Django.
Right?
If any of you haven’t seen it, watch that video first. Right?
So you will get an idea about how it works.
Okay?
So we will use Delete method here.
Why delete method?
Because we have to perform Delete query.
Okay?
So how will we do it?
I’ll show you practically how it works.
Okay? Okay.
So what will you do first of all?
We’ll make a view here.
What will we do?
We’ll make a view here & use that view to delete.
Right?
I’ll comment here, which is Delete Data View.
We’ll make a function here.
DeleteData request & use primary key here.
Why will we use the primary key?
Because we want to delete a particular user only
We don’t want to delete all the users altogether, the particular user which we will delete, only that user’s data will get removed.
Okay?
First of all, we will need to get the ID of that particular user.
How will we do it?
I took a D-Data variable here. Okay?
Student.objects.get.
And ID = primary key, that is we passed pk.
The way we passed it above in update, in the same way we passed in delete as well.
Right?
So now we got that particular user’s data in D-Data.
Now we will write a Query for Delete here.
How do you have to write?
It is simple only.
We have to use the D-data.delete method.
Okay?
After it gets deleted, which page should open up?
The show page should open up.
We have to render it, redirect to showpage only.
Right?
We will paste it here.
So we made this view which will be used for delete.
Right?
So we made this view to delete and now we will make?
We’ll make a url.
Right?
So let’s go to url & make a ‘Delete URL’ which is path, delete int : pk views.DeleteData, then Name.
What did I take here?
I took Delete.
Okay? Right?
Clear?
Now I copied this name, and then used the delete button from show.html here.
See, we made a Delete form here.
We will pass it in its action only.
Pass what?
The name of the delete url we created.
Url this and we’ll pass the primary key = i.id.
Okay?
Right?
We have to do only this much for Delete, nothing else.
And here, when you refresh your showpage, as soon as you click the delete button…
Suppose I want to delete the first ID.
Just as I click on the Delete button, you can see it got deleted.
If you check here, you can see that the first data has been removed.
Now the IDs left are 2, 3 & 4…those same IDs will be visible on the Showpage as well, 2, 3 & 4.
Right?
So this is how Delete works in Django.
Okay?
Understood how CRUD operation works in Django?
Where first of all we saw how it is inserted, how the data is shown, how that user data is updated and then how the data is deleted.
Okay? Right? Clear?
Understood how CRUD operation works in Django?
Right?
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
In the next video we will see how to integrate Registration Template,
Means what we will do is, we will find a Registration Template of Bootstrap & integrate it in our template integration.
Okay? So see you in the next video.
Share a personalized message with your friends.