WEBVTT

00:01.280 --> 00:01.730
Okay.

00:01.730 --> 00:05.540
In this video we will create a view based on class.

00:05.540 --> 00:10.910
So that will help you understand how the actually actual Django solution works.

00:10.910 --> 00:18.800
So in our view, we have a simple function here, and that's just a function that will return Http response.

00:18.800 --> 00:22.280
So let's try to do something similar with a class.

00:22.280 --> 00:28.340
So I will come here and I will do class here and then I can name it.

00:28.340 --> 00:31.850
I will do an order by by convention.

00:31.850 --> 00:34.610
We start class with a capital letter here.

00:34.790 --> 00:44.510
So we will create a class that will have some functions inside and then we can reference that class

00:44.510 --> 00:45.860
in our.

00:47.340 --> 00:52.110
URLs here, so I will use that class for displaying something.

00:52.110 --> 00:56.790
So we will use something like we use it here, but using the different syntax.

00:57.900 --> 01:00.360
First, we'll need to import views.

01:00.630 --> 01:03.450
So from Django.

01:05.010 --> 01:07.230
Views import?

01:09.310 --> 01:09.730
View.

01:10.210 --> 01:18.400
And then this view will insert inside the parentheses here, which is what we are saying here, is for

01:18.400 --> 01:25.210
that class use whatever is in that view, and then everything will be available for us.

01:25.210 --> 01:33.310
So basically what we can do is we can kind of do similar thing what we've done with our normal function,

01:33.310 --> 01:35.260
but this one will name get.

01:35.680 --> 01:40.810
So we have request as well and then we respond will return something else.

01:40.810 --> 01:47.920
So this is another function inside class.

01:49.620 --> 01:52.710
So we have a different output here.

01:52.710 --> 02:01.260
And basically what we are doing is we are having a get function inside our class so we can do self here.

02:01.290 --> 02:07.080
That means we reference actually this class if we need to use it inside our function.

02:07.080 --> 02:12.210
The first argument here is always a self, which is this class what we have.

02:12.240 --> 02:19.440
So at the moment we have our another class available and let's go to the URLs and we'll just duplicate

02:19.440 --> 02:20.280
this line.

02:20.280 --> 02:21.840
We'll put a comma on this one.

02:21.840 --> 02:30.360
So it will be a array of different paths so I can do another, which is just a name for URL.

02:30.360 --> 02:35.490
And then here what I can do is I can do another.

02:36.090 --> 02:39.000
Actually I need to go, I will import that.

02:39.030 --> 02:40.080
So from.

02:41.640 --> 02:44.910
Dot views, import and other.

02:44.910 --> 02:51.750
So we'll just import that class from the file views and then I can use this another reference that another

02:51.750 --> 02:53.880
and then I can do as view.

02:54.510 --> 02:58.770
So treat this class as a view if I will save it.

02:58.770 --> 03:02.550
Now I think we have indentation problem.

03:04.420 --> 03:09.040
And Python is just very picky.

03:10.210 --> 03:14.050
So we need to have indentation here and two indentations here.

03:14.050 --> 03:17.230
I will save it and then I will run it again.

03:19.250 --> 03:20.270
And let's try it.

03:20.750 --> 03:24.260
Let's try our new URL, which is another.

03:26.750 --> 03:31.040
I can do that with first function and then I will try another.

03:31.040 --> 03:35.000
And you can see here, this is another function inside class.

03:35.360 --> 03:37.490
So I can go here.

03:37.490 --> 03:42.380
I will make it a little bit bigger for you to see the URL.

03:44.130 --> 03:51.460
So what we have here is the another is pointing to our class that we've created inside views.

03:51.480 --> 03:58.170
So views is a class and then we have a method get and that will response this text, which is this one.

03:58.170 --> 04:02.100
We still can use this normal function as we did before.

04:02.100 --> 04:05.070
So both of these will be available.

04:05.070 --> 04:11.520
So first and another, let's try first you can see first message from The View.

04:11.550 --> 04:13.530
This is first message from The View.

04:13.530 --> 04:16.380
So we have two different options to use.

04:16.380 --> 04:19.560
We can use normal function or we can use class.

04:19.590 --> 04:27.480
Class is more complex option and you need to write a little bit of more syntax to have it, but it will

04:27.480 --> 04:30.630
give us much more flexibility.

04:30.660 --> 04:36.450
Basically, in that view, what we have is we already have a lot of things included.

04:36.450 --> 04:44.800
So if I will hold command or on my keyboard and click on The View, you can see here we already have

04:44.800 --> 04:49.780
different methods available for us and then we have other things here.

04:49.780 --> 04:58.120
I don't recommend reading this if you are not a really experienced Python developer and you know a little

04:58.120 --> 05:04.030
bit of Django, but this is actually what we include in our class.

05:04.030 --> 05:12.130
So we we are telling Django or telling Python that include whatever is available on this class which

05:12.130 --> 05:15.310
is the built in Django views inside our class.

05:15.310 --> 05:22.150
So everything is available like the method gets or like the method as view as we have here.

05:22.150 --> 05:27.510
So later on I will show you what's actually included in the classes.

05:27.510 --> 05:36.970
So we'll use model view class, which is has more functionality built in, but basically that's how

05:36.970 --> 05:37.930
we do it.

05:37.960 --> 05:45.820
We might have on the classes more options so we can decide we can have some attributes here and some

05:45.820 --> 05:54.040
other functions and we, we can reference this all options and we can also reference the class itself.

05:54.130 --> 06:03.580
So that's how we can write views with classes and we will extend that knowledge later on for having

06:03.580 --> 06:05.230
full Django solution.
