Welcome back, everyone, to this lecture on the creative class based view.
Now, before we continue, I do want to point out that the next few lectures are going to highlight
what I think is one of the best features of class based views, and those are what I like to call the
model based, class based views.
These are the ones that directly connect with their model.
So there's a couple of operations that are super common when you build out a model on the back end,
things like creating a new instance in that model, maybe a detailed view for a particular instance
of that model being able to update, in instance, delete an instance or lists a bunch of instances
or all the instances from any particular model in your database.
What Django provides for us are class based views that automatically create the appropriate views,
forms and context objects for predefined template names by simply being connected to a model.
And these classes only require a few attributes and then they automatically do the work for you.
It's pretty amazing how much time these set of classes can actually save you.
As a quick, important note, because the classes are designed to be simple, that is simple to use.
These views require a template name to follow a very specific pattern.
For example, the if you were about to discover is the create view, and that actually requires you
to create a template name in the specific pattern of model underscore form that each HTML.
So for example, if we created a teacher model, then our template name has to be teacher underscore
form that each HTML.
I just want to point this out because this factor is often confusing for students because it seems like
Django just magically knew a template HTML file name existed.
But really, what it's doing is just looking for a specific naming convention pattern.
OK.
Let's begin our journey of model based lives by starting with Create View, which automatically creates
a view to create a new instance of a model class by the client on the user side.
OK, so here I am inside a visual studio code as a quick note, recall that previously we created a
contact form view and we saw that I could easily connect a form and then actually have a class based
view for that.
We've also known that there's technically something that exists called a model form.
So you may have been thinking, Oh, this is super convenient, I'll simply create a model form and
then connect that with a class based view.
Now, obviously, that is apparent enough that jingo figured it would do that work for you.
So in the same way, you can use a model form to quickly connect a form to model and then technically
use a form view to connect to that model form.
You could just skip all the steps and directly connect a class based view to a model.
The first one we're going to discover is the create view.
Go ahead and import create view from the jingo that views generic.
And then I'll show you right now how to set up a create view.
So underneath, thank you view, I'll create a new class.
And this is going to be called teacher create view.
Technically speaking, you can call these classes whatever you want, but I would highly recommend you
follow the convention of the name of your model and then the name of the template view or whatever your
view happens to be, like create, view, form, view, etc. So we're right now discovering the model
based, class based view.
So here we have the model name and then create view.
I'm going to inherit, create, view.
And then there's just a few attributes that we need to connect it to, and he first need to tell it.
Well, what model are you actually going to connect to?
And I'm going to connect it to the teacher model.
Now, no, I have an imported teacher model yet, so I need to do that at the top of my script here.
So I'm going to say from.
Classroom thought models in poor, the teacher model that we created earlier, so recall that we've
already created the teacher model inside models that Pi first name, last name, the subject they teach
and there's a string representation of that model.
OK, so coming back to our views.
They were go.
Coming back to our views, step one, connect it to the model, step two for create view is to figure
out what field do you actually want to display in your create view, and this looks really similar to
model forms.
So it's essentially kind of the same attribute.
If you want, you could just pass on a list here.
That's something like, OK, first name field.
Also do the last name field, et cetera.
Maybe there's some fields you don't want the user to have access to, but typically you'll often just
say, I want all the fields to show up in my create view.
So you just do what we did in the model form, which was underscore, underscore, all underscore,
underscore.
OK, so you connected it to the model, you connected it and said, I want all the fields to show up
in this template when they're actually creating that form.
And then finally, remember, this is technically a creation view.
So this is going to be a form that shows up in order to create a new instance of a teacher.
Now, obviously, when you hit submit on that form, we should probably take you to a success URL.
So that's the other attribute we need to define success URL.
And let's just take them to the thank you page again.
We already know how to do that based off our example here, which was reverse lazy classroom.
Thank you.
So it's simply copy that and paste that in.
And that's pretty much all we really have to do to get great view working again.
You simply connect it to a model, tell it what fields you want to show up and the template when it
passes back that creation form and then the success URL, which in this case is just reverse lazy and
then the URL you want to point after they hit submit.
Now again, a question students often ask is how does it actually know what template to connect to notice?
I didn't actually say template name.
Well, create view by default.
Once you actually tell it the model the template name is going to look for is the model underscore form
that each HTML file inside the same application that the views is in.
So what we need to do is create that particular template and the model is going to be in all lowercase.
So it's going to be lowercase T here.
And so we'll come back to classroom, create a new file and call it teacher underscore.
Form the HTML and now Django knows I'm going to look up for model underscore form and find the connection
here.
And then it's kind of up to you where you want to put for a header here.
I'll just say teacher form.
I'm actually not going to specify create form because you're going to see other views are actually going
to share this same template.
Because imagine if you wanted to update a teacher, it would actually be useful to just use that same
form.
And then we use a form past that in.
I'm going to say the method is equal to post.
And then we pass the necessary token.
And then pass in form that as.
And then we simply need our input button.
So we'll say input submit.
I'll put the value of the text as submit.
And there you go.
That's basically all you need.
Automatically, what's going to happen is teacher create view looks up this particular template.
So in that case, teacher underscore form and it figures, OK, these are the fields you want.
I'll pretty much auto create a model form for you, and then we'll set that up.
And what's really cool about this is the second you hit the submit button, it's pretty much automatically
going to hit save.
After all, the fields are validated, so it's doing a lot of work for you.
But the caveat with that work is this view is really only for one thing, it's creating a new instance
in that model.
Let's explore how this works.
So I'm going to come back to my home page.
So we're going to come back here.
And in fact, let's link it to our home page.
So I'm going to come back to home here, and let's just copy and paste this.
You'll see we're going to be like gradually expanding this and let's say classroom contact and actually
connect to this create view to a URL, yet only to do that as well.
So we'll actually say contact and this will be called creates, let's say, create teacher.
And this will be Create Teacher page.
So create teacher and just to specify creates new teacher.
OK, so then what I'm going to do is come back to you URLs and imports that teacher create view.
And let's set it up as a new view here, so we'll say Path.
I'm going to call this creates teacher.
I call my teacher create view as view.
And let's give it the name that we said was create underscore teacher.
OK, so now I have this class based view, which is a create view, I connected it to a model.
I told it what fields I wanted and I told it where to go after I successfully submitted a new teacher.
And what that's going to do for me is all the heavy lifting of inserting this new teacher into the database.
And really, keep in mind, it's just doing it from three attributes here, which is really incredible
at how much time is going to save you later on.
So I'm going to come back to my home page here.
Let me refresh.
Let's make sure we get a typo here.
Create new teacher page link.
I click on that.
That takes it to the URL classroom forward slash create underscore teacher.
And then let's create a new teacher.
So I will create a new teacher here, and let's actually make the subject go.
OK.
So what's really interesting is you have now this form it's automatically created for you.
Remember Insight Teacher underscore form.
I'm basically just passing in form and simple view.
Obviously, you could style this however you want it, but that's all you had to do at the minimum.
And then when you hit submit, it takes you to the thank you page.
Now what I could do is actually confirm that that teacher has been created in order to confirm that
the teacher has been created.
I will need to either create a super user and then go to the admin to view that it's been created.
Or I'll need to create another view that ends up listing all my teachers.
So what you can do if you want, if you kind of don't believe that it worked is right now just create
a super user in the admin.
But what I'm going to do is in the very next lecture, I'm going to show you the next model based view,
which is a list view in order to list all the entries for a particular model.
So hold off on this right now.
Just take it on my word that it actually worked, but I'll prove it to you in the very next lecture
through a list of you.
I'll see you there.