Welcome back, everyone, in this lecture, we're going to explore the elite view, which allows us
to delete an instance from a model.
All right.
Here I am inside our views that PI file in order to create a delete a view, we need to import that.
So we'll say again from jangled that views generic import deletes view.
This is a kind of a simple view.
Basically, you just pointed to a primary key and then it has a one button form that says, Are you
sure you want to delete this particular instance of the model?
So let's set it up.
We're going to create a class here, and I'm going to call it my teacher.
Delete a view, and then we inherits from the view, we connect it to the model.
And then keep in mind, the elite view is technically a form, it sends back a form that just has a
single confirmed delete button because keep in mind if you're already on the deletes view, you're not
looking to actually read all the fields of information, like on a detailed view or change any particular
aspect.
You basically just have a button, says, Hey, you're looking at this particular teacher, are you
sure you want to delete them?
So we're just going to say form go to confirm delete button, and we just simply connect it to a teacher.
But because it is a form, we also need a success URL.
I'm going to copy the same one we used for up the view because after deleting the teacher, we'll want
to confirm that by going to the list of all the teachers.
So we'll go ahead and save that change there.
OK.
So we have our daily view working.
Let's connect it to our URLs.
Will come to URLs.
Remember to import teacher, delete, view and then create a new path here.
And it's kind of up to you what you want to call this path.
I'll call it deletes, underscore teacher.
But remember, we don't want to just delete any old teacher.
We have to delete a specific teacher.
So we say integer primary key.
Just read that for update and detail.
And then we pass in teacher delete to view as a view.
And if you want, you can give it a name.
We don't use a name here.
OK, so we have our delete teacher and then we need to set up our actual template.
So the way this works is this once something you will kind of need to remember is a delete view.
The default template name that it looks for is going to be your model underscore, confirm, underscore,
delete the HTML.
So in our case, that's going to be creating a new file with the name teacher underscore, confirm,
underscore, delete the HTML.
So that's what it automatically looks for.
And in this case, you basically just have to create a form and inside that form, it's going to be
a post method.
So we'll say method is equal to post.
And you need to pass in the seats are a token for security.
And then you have a submit input button, and here maybe you want the value instead of being submit
the value of the actual text inside the button to be confirm.
Like delete or something like that, and you should probably have some information here of saying something
like, are you sure you want to delete this teacher?
And what's also nice about the delete view?
It does send back an object of the teacher so you can actually grab information from it.
So if you want it to, you could just to inform the person viewing this page of the teacher.
OK.
So that's why it's kind of similar to a detailed view and the fact that it is sending back this teacher.
Except it also sends back the idea of this form that can then have this confirmation of the leak notice,
how it doesn't actually send any labels or inputs.
It's just a form that says, Hey, why don't you submit this?
I'm going to go and grab this teacher in the database and then remove them through a delete call.
So this is teacher underscore, confirm, delete.
And that is how the elite view works, you connect it to a model and then take it to a success URL.
And remember inside this URLs, I need to say delete feature with that particular integer call.
That is the primary key.
Then the question arises is where do we actually want to link to the teacher?
Well, it probably makes sense to link it in the same way we linked to Update Teacher.
So I'm going to go back to my teacher list view, and in the same way, I had this unordered list to
update the information for the teacher.
Let's create another nested unordered list here.
So we'll go ahead and essentially copy this.
And pasted again.
So we'll put it in here.
And in fact, this actually doesn't really need to be nested again or it's going to create another list
item inside that same unordered list.
There we go.
So we'll have the teacher and then nested inside will be, Hey, do you want up the information or do
you want to?
Delete that teacher, so delete.
And then save that.
OK.
So what this is saying is go ahead and go to delete teacher URL for that particular teacher I.D., which
means it would take you to the late teacher URL for that particular I.D..
Which brings back a teacher delete view, which we know just connects to the teacher model and calls
model confirmed.
Delete that email, which we have here, that basically says, Hey, are you sure you want to delete
this teacher?
Here's the teacher going about to delete.
Here's the form.
Click Submit.
And I'll go ahead and execute the deletion of that particular instance of the model.
So let's check this all out.
Make sure we didn't commit a typo.
Then it refresh this.
I'll go to this teacher page link and here it, says Adam Smith up the information for Adam Audley Adam.
Obviously, we want to use some sort of more like secure coloring or really, it depends on your website
of how you actually want to connect the deletion.
But let's go to the third teacher here.
I'll click Delete Third and it says, Hey, are you sure you want to delete this teacher?
Third Teacher teaches math and I'm going to confirm delete and you notice it no longer shows up there.
Let's go ahead and delete Adam Smith.
That says, hey, are you sure you want to?
The teacher, Adam Smith, teaches math and again, it makes sense that the form is just a single button
form because what else information do you need to provide?
It already knows the primary key automatically through the use of the class based view.
So you click the lead and then boom.
OK, so that's really it for our class based views.
A very, very powerful tool.
Again, you have the ability if we just kind of go back here through class based views, two in two
lines connect to a template awesome through essentially three lines.
Connect to a creation view where the creation is just a form.
Then you can also again through three lines of attributes, just list all the instances and you have
the flexibility here for the query set of how you want the list them.
You have the ability to show a specific detail to update a specific instance and then delete a specific
instance.
And those are the key model based, class based views that are going to make your development life on
The View's side of thing a lot more smoother.
Comfortable.
So we have the template, form, view, create, view, list, view, detailed view, update, view
and The View.
I would highly recommend that you use these as often as possible.
The last caveat being is not every single view that you could possibly think of is ideally a class based
view.
There may be times where it just makes a lot more sense to write the custom function and take care of
it that way.
So this is also laid out in the Django documentation that there is not some magical class based view
that exists for your specific situation.
If it comes down to it, you can always go back to the function based view and add in a lot more logic.
Remember, with the simplicity and ease of use of the class based views comes a lot more constraints
and caveats.
And if you ever find yourself outside of those constraints, you can always go back to the function
based.
You hope that was useful for you.
I'll see you at the next lecture.