Welcome back, everyone, in this lecture, we're going to expand their knowledge of generic class based
views by looking at the form view.
Let's check it out inside the code editor.
All right, so here I am back inside Visual Studio code.
Now we just saw how to use a generic template view to easily connect a template that already exists
to a class based view.
We'll also now discover is that there is a form view that easily allows us to connect a form to a template.
So let's go ahead and import the generic form.
View.
And then what we're going to do is create a new class and in order for this class to actually make sense,
we should create a form.
So eventually we're going to create a contact form.
I'm going to say contact form view.
And then pass it or inherit form view for right now, I'll just say pass, because we will fill this
out later.
So let's create quickly a form that maybe we want the user to fill out for contacting us.
Maybe they leave a name and a message.
So we're going to create a new file.
And let's make it form stop high, and we'll say from.
From jingo import forms.
And we'll create a very simple contact form forms that form.
And remember, this has failed, so we'll have a name field, which is a let's say, character field.
And let's create a message which let's also have it just be a character field, except let's give it
a text area, which it will say which is equal to forms thought.
Text area.
OK, so super simple form.
We've already seen how to connect jangle forms to function based views in the previous section, but
now we're going to see how to do it with a class based view.
And as I mentioned, the generic views actually can take care of this for us.
So we need to do is come back to views so we have views that pay.
And we're now going to need to import that view that or that form that we just created.
So I'll say from classroom that forms import and we have now imported that contact form.
So how does a contact form view work?
Well, we saw that a template view has a single attribute, just essentially connect it to the correct
template.
This one is going to be slightly more complex, although it's still pretty simple.
What you do is you connect it to the form class.
For that, you choose form, underscore class and then connect it to the actual class.
Note that you do not actually create an instance of the form.
You just say, OK, the form class.
This view will be connected to its contact form.
Then you decide on the template name, but this form should show up it.
So here we're going to say is create a new file and let's call this contact for each HTML and inside
contact our each HTML.
What I'm going to do is have a very simple header here that says form the template just so I know what
I'm looking at to contact the HTML.
And then we create a form.
And here will say the method is equal to post.
And then what's going to eventually happen is I can actually pass on a CSR token and a form.
So I can say form.
And let's have this be as paragraph, and then we'll need a submit input, so we'll see input.
Submit.
All right.
And this should look basically the same as we would expect a function based view to look like.
However, we're going to see that it's actually a lot easier to set everything up on the class side
things.
So we're going to say set up this contact each time, I'll know it's already expecting a form to be
passed to it.
So we say, OK, we're going to pass on a form to this.
So we have the form class and I'm going to connect it to the actual HTML file we just created.
So this luckily has the same attribute name as before, which is template name.
Except this time I'm going to say classroom forward slash contact HTML.
OK.
So what this does is it connects the form itself to this form view and the template name, but there's
a couple more things that we need to expand for.
Form views The two main things you need to do is where should we go after the successful form is validated.
So there's something like what should we use or should we go to?
If it was successful, then we also need to do a calculation on what to do with form.
So let's actually take care of the successful URL first.
So again, that's actually also an attribute.
You just say success underscore URL, and you should note that it has to be these attribute names.
These are inherited from the view you're inheriting from.
So form class is the form template name is the template HTML success.
Your URL is the actual URL you want to go to.
Now this would actually depend on the URL that you set up for your maybe success.
And let's have it go to the thank you page.
So for that, that would be if we go back to your URLs, remember that's going to thank you.
So we'll come back to views and we'll say.
Classroom.
Forward slash, thank you.
Note that this is and what's sometimes confusing to users is that this is a URL.
It is not a template that each HTML file.
Notice the difference.
It's success.
You are ill.
So what's the actual URL at the top in your browser you want to go to?
It's not the actual template you want to go to.
Later on, I will show you a way of looking up based off the URL name, but for right now, I just want
to make it very clear that the success URL is the raw URL you're going to go to.
Not the template you want to connect to.
So notice how I didn't say here, thank you HTML or something like that.
It's just the actual URL at the top.
So we have a form class.
We have our template name.
We know where to go to.
Upon submitting that form.
And now what to do with the form.
And this is actually now a method inside form view.
And that method needs to be called form underscore, valid and it takes itself and it takes in form.
Form being the connection to what we have here.
This form aspe and then this is essentially called when a valid form has been posted.
Essentially, the methods post and what it does, it returns an h to the peer response.
So what we can end up doing here is whatever you want with the clean data.
So if you wanted to, you could do something like form, dot, save and save that to somewhere in your
back end.
What I'm going to do right now is I'm just going to print out form dot clean data.
Just to show you that, OK, if you wanted to, you could kind of do whatever you wanted here and then
what you end up returning is.
A super call, which has to do with class inheritance.
And then you call form, underscore valid and pass in the form, you can think of this last line as
kind of similar to when you had something in your function based view that looks something like this.
So essentially this remember we did this kind of passing the post request to the form and created you
and says the form.
That's essentially what this whole method is doing, but it's just doing it in a very quick line.
It says, OK, the form is valid.
What do you do with it?
And then use this method call inherited from form you to essentially take care of things.
So right now, I'm going to be printing out the form clean data.
Go ahead and save those changes.
We still need to actually connect our contact form view to our URLs, so we'll come back to your URLs
that pie.
And now let's import that contact form view, and then I'll pass it in as a new view here.
So we'll see path.
And this is going to go to contact path.
Comma, it is the contact form view.
We need to remember, say, as view, and then if you want, you can give it a name like contact.
OK, now there has a name I can actually link this on my homepage.
So we'll come back to home page and let's just copy and paste this here.
And instead of classroom thinking it's going to be a classroom.
Contact.
And now this is going to say.
Contact form page link.
Save those changes.
And now let's make sure this is all working.
So you may need to because we're adding forms and stuff.
Rerun your server here, and let's make sure we don't make any errors or issues.
So I come to home HTML.
I click on Contact Form Page Link.
I now have this form view template on contact each HTML.
Notice how the forms are being passed back here, so I can say something like a message is school is
cool and then I can hit submit and it takes me straight to the thank you page.
And let's make sure that our data was actually printed out and it's right here.
Jose Message School is cool, and obviously if you wanted to, you could do whatever you want inside
of you.
So this is just where your logic would go.
We'll go inside this method call, but that's how a form view works.
It's typically a lot simpler than the more complex, function based view for a form.
So I want to quickly go over the main steps of what we had to do to use a form view for the generic
form view.
OK, step number one is you need a form.
So you create whatever form you want here forms that form.
If you want, this could be a model form, but I would say hold on that later because coming up next,
we're going to have template views that directly connect the model, essentially saving you the step
of even making a form.
And then once you have your form ready to go.
You come back the views, you make sure you import that form and then you also make sure you import
formed you as a generic view and then the key attributes in your remember are OK, what's the form class
or connecting to?
What's the template name you want to send that form to?
Our case it was contact table.
And then what is the success URL that you're going to send them to?
And remember, it's a URL, not a template.
It's the actual URL path that's going to appear at the top of their browser.
That's why I had a say classroom for thank you, because that's what if we come back to your URLs?
That's what this is.
It's classroom for us.
Thank you.
So you're essentially connecting them to that particular pathway.
OK.
And then once you've done that, you can use this form valid method that pretty much the top and the
return statements are always going to look the same, but it's up to you, whatever you want to do inside
form clean data.
So remember, this is a dictionary, so you could always do something like, OK, call the name that
was passed in on that and then maybe save it somewhere or send an email or do some sort of action there.
All right.
Now I did mention there's some logic or functionality you can use to not need to remember this entire
success URL.
Remember, previously we imported a function called reverse.
So we were able to do something like this from Django that URLs.
Import reverse now reverse is going to just give you back the actual name for the URL you're looking
for.
So for example, I could say I want to figure out what's the view associated classroom.
Thank you, for example.
So remember, this would actually give you back the view.
But in this case, we actually need the URL for that.
So luckily for us, there's.
Reverse lazy, and I do have a link for you, insider notes on what the differences between reverse
and reverse lazy and Django, often that's a point of confusion of students.
So you can check out this StackOverflow posts on explaining the difference between a reverse and reverse
lazy.
They do have kind of some code here to explain.
I think the best explanation really is that honestly, if you're just being simple here, reverse is
going to return a string and then reverse lazy returns and object.
But really, what you should be thinking of here is if you're using success URL, you use reverse lazy.
If you're reversing inside a function, you can use reverse.
These are probably just the easiest number one and number two here.
So just keep in mind you're using class based views.
You're using a success URL.
Just use reverse lazy.
You're using functions you want to reverse inside a function.
Use a redirect reverse combo, as we've seen before.
OK.
Netlink for you is a notes.
Or you can just Google search difference between reverse and reverse lazy and Django.
But put simply, it's essentially just that you're using class space views just from rotis, reverse
lazy and then you come back down here instead of having to remember classroom forward slash.
Thank you.
You say reverse lazy and then pass in classroom and then thank you like that.
And that should still work.
So let's confirm that it still works.
I'm going to go back to the home page.
So here I am at the home page.
Let me refresh this contact form page.
Let's say the name is jam and the jam jam message.
And then thank you.
And it's still working.
And then I printed out that first name, Jim.
There it is.
OK.
That's it for the form of you so often form views, you use them a lot with models, but there's actually
generic template views that already automatically connect to models for us.
So it's going to get even simpler than doing something like this.
I'll discuss that in the next lecture.