Okay.
In this video we will create a view based on class.
So that will help you understand how the actually actual Django solution works.
So in our view, we have a simple function here, and that's just a function that will return Http response.
So let's try to do something similar with a class.
So I will come here and I will do class here and then I can name it.
I will do an order by by convention.
We start class with a capital letter here.
So we will create a class that will have some functions inside and then we can reference that class
in our.
URLs here, so I will use that class for displaying something.
So we will use something like we use it here, but using the different syntax.
First, we'll need to import views.
So from Django.
Views import?
View.
And then this view will insert inside the parentheses here, which is what we are saying here, is for
that class use whatever is in that view, and then everything will be available for us.
So basically what we can do is we can kind of do similar thing what we've done with our normal function,
but this one will name get.
So we have request as well and then we respond will return something else.
So this is another function inside class.
So we have a different output here.
And basically what we are doing is we are having a get function inside our class so we can do self here.
That means we reference actually this class if we need to use it inside our function.
The first argument here is always a self, which is this class what we have.
So at the moment we have our another class available and let's go to the URLs and we'll just duplicate
this line.
We'll put a comma on this one.
So it will be a array of different paths so I can do another, which is just a name for URL.
And then here what I can do is I can do another.
Actually I need to go, I will import that.
So from.
Dot views, import and other.
So we'll just import that class from the file views and then I can use this another reference that another
and then I can do as view.
So treat this class as a view if I will save it.
Now I think we have indentation problem.
And Python is just very picky.
So we need to have indentation here and two indentations here.
I will save it and then I will run it again.
And let's try it.
Let's try our new URL, which is another.
I can do that with first function and then I will try another.
And you can see here, this is another function inside class.
So I can go here.
I will make it a little bit bigger for you to see the URL.
So what we have here is the another is pointing to our class that we've created inside views.
So views is a class and then we have a method get and that will response this text, which is this one.
We still can use this normal function as we did before.
So both of these will be available.
So first and another, let's try first you can see first message from The View.
This is first message from The View.
So we have two different options to use.
We can use normal function or we can use class.
Class is more complex option and you need to write a little bit of more syntax to have it, but it will
give us much more flexibility.
Basically, in that view, what we have is we already have a lot of things included.
So if I will hold command or on my keyboard and click on The View, you can see here we already have
different methods available for us and then we have other things here.
I don't recommend reading this if you are not a really experienced Python developer and you know a little
bit of Django, but this is actually what we include in our class.
So we we are telling Django or telling Python that include whatever is available on this class which
is the built in Django views inside our class.
So everything is available like the method gets or like the method as view as we have here.
So later on I will show you what's actually included in the classes.
So we'll use model view class, which is has more functionality built in, but basically that's how
we do it.
We might have on the classes more options so we can decide we can have some attributes here and some
other functions and we, we can reference this all options and we can also reference the class itself.
So that's how we can write views with classes and we will extend that knowledge later on for having
full Django solution.