WEBVTT 00:00.090 --> 00:06.110 Hello everyone and welcome to this new lesson about the EPA view class in the first lecture of this 00:06.110 --> 00:06.980 section. 00:07.050 --> 00:13.140 We have mentions that January's framework uses many of Jangles programming conventions and one of the 00:13.140 --> 00:20.070 most striking examples is its use of class based views as we've pure Django. 00:20.190 --> 00:26.670 Using this kind of pattern makes it easy and fast to create reliable web apps and by reusing common 00:26.670 --> 00:32.970 functionalities it also helps us keep our code dry which means in line with they don't repeat yourself. 00:32.970 --> 00:38.670 Principle of software development in this lesson we're going to start understanding our glass based 00:38.670 --> 00:45.260 views are used in the context of REST API development by using django as frameworks API view class. 00:45.480 --> 00:50.220 Even though at a first glance the code we will write might look similar to the code we have already 00:50.220 --> 00:51.860 written in the previous lecture. 00:51.870 --> 00:57.750 It is important to note that the API view class is the class upon which all the generic class based 00:57.750 --> 01:00.270 views that we'll introduce later are not based. 01:00.270 --> 01:03.890 Therefore it's important to make some examples of its use. 01:03.900 --> 01:06.020 Let's get started. 01:06.330 --> 01:06.600 Okay. 01:06.630 --> 01:08.640 So here we are invisible to your code. 01:08.760 --> 01:13.300 And the first thing that we need to do is to update our import statements. 01:13.500 --> 01:19.260 And that's of course because we need to import the API view class but we're then going to use to create 01:19.260 --> 01:21.540 our class based VBA views. 01:21.780 --> 01:29.060 So from this framework that views import API view. 01:29.130 --> 01:35.760 Let's now take a look at the class definition took it the understanding of how awesome this class release 01:36.320 --> 01:41.850 is you can see we got a bunch of methods as one might expect. 01:41.850 --> 01:49.670 And that's because our class is particularly powerful because of all three properties that we can set. 01:49.680 --> 01:57.390 See we got a lot of properties such as the Renderer classes but asset classes but also what integration 01:57.390 --> 02:02.010 classes throttle classes per mission classes and so on. 02:02.010 --> 02:08.880 Now we can define to personalize automatically basically Debbie a viewer of our class now as a matter 02:08.880 --> 02:16.410 of fact we are not going to use these parameters these attributes right now and that's because as we've 02:16.440 --> 02:23.760 said this class is used as a base for a lot of other classes that we're going to use in great details 02:23.970 --> 02:31.110 later on during the course the purpose of these specific lecture is to give you an introduction on how 02:31.110 --> 02:39.180 we can actually create API views using django as framework so that then later on as we introduce all 02:39.180 --> 02:45.060 the other classes that are based upon this code you're going to be able to get a proper complete and 02:45.060 --> 02:52.700 deep understanding of our day work so we're not going to write two classes article least create GPA 02:52.710 --> 02:55.410 view and article detail API. 02:55.440 --> 03:01.380 If you basically will go into right to the same kind of functionalities we're going to write down the 03:01.380 --> 03:06.570 same functionalities this time using classes and we have the same functionalities clearly. 03:06.720 --> 03:12.330 I mean the same functionality that we've developed let's say in that previous lecture. 03:12.330 --> 03:17.720 So I'm going to go right below here and I'm going to define our first class. 03:17.760 --> 03:18.840 So class. 03:19.440 --> 03:27.380 Article list create API in view which is going to extend our API view class. 03:29.040 --> 03:32.510 Like so and to provide functionality to our class. 03:32.580 --> 03:39.360 This time we're going to use class methods and each method is going to correspond to the H GDP verb 03:39.600 --> 03:42.610 that is actually going to power our End Point. 03:42.640 --> 03:50.920 Okay so we're going to have a get method which is going to take self of course and the requester. 03:51.540 --> 03:53.190 So the code is going to be the same. 03:54.450 --> 03:58.050 We've written right here. 03:58.050 --> 04:00.960 Like so when the fixed indentation. 04:01.030 --> 04:01.940 Perfect. 04:02.230 --> 04:11.720 And then I'm going to define a post method which is going to accept self and request. 04:14.160 --> 04:19.460 And the code is going to be basically the same that we've written up here. 04:19.700 --> 04:26.760 Like so so even though the code is basically the same that we've written with the function based API 04:26.760 --> 04:34.520 views can see out now it's more organized the class definition makes it more concise. 04:34.590 --> 04:46.690 Let's now write the detailed view straight away so class vertical detail API view. 04:46.940 --> 04:56.980 She's going to extend clearly maybe a view like so and considering that up here you see we've actually 04:56.980 --> 05:02.070 written a try except the blocker right here on the top of our function. 05:02.260 --> 05:10.810 We can now define a method a get object method which is going to return the instance that we are looking 05:10.810 --> 05:11.740 for. 05:11.740 --> 05:20.500 So def get object which is going to accept self and a primary key like so. 05:20.500 --> 05:27.850 So particle equals and considering that we are making an example of how to use the API view class this 05:27.850 --> 05:33.010 time we can actually use the get object or form for a shortcut. 05:33.010 --> 05:34.120 So I'm going to import it 05:37.230 --> 05:50.250 from the last framework not generics import get object or photo for therefore particle is going to be 05:50.250 --> 06:03.360 get object or for a form of article with primary key equals primary key and we are now going to return 06:04.290 --> 06:12.260 the article instance and we can now write our mind so so def do get method which is going to accept 06:12.300 --> 06:15.930 self request and a primary key. 06:16.200 --> 06:24.300 So first of all we need our article instances so article equals self doubt and get object. 06:24.350 --> 06:27.570 Now we're going to pass it the primary key. 06:27.720 --> 06:32.420 We can then call civilized at every turn. 06:32.760 --> 06:39.780 It's data like so we can do something similar for the other methods. 06:39.780 --> 06:47.730 So Def put up which is going to accept self request and primary key and as a matter of fact is going 06:48.480 --> 06:57.550 to need in the article as well we can then go ahead and copy the code we've written right here 07:01.430 --> 07:03.570 like so perfect. 07:03.830 --> 07:13.160 Same thing we can now do for the delete metal so def delete which is going to accept self request and 07:13.160 --> 07:22.330 the primary key we get not after instance we can now delete things. 07:22.330 --> 07:32.930 This like so and threat to earn an appropriate response HDB what for not content. 07:32.970 --> 07:40.210 So now that our views are ready we can go ahead and define the two corresponding endpoints. 07:40.230 --> 07:49.380 I can't comment this impulse statement right here and I can use the same two endpoints as a base to 07:49.380 --> 07:53.400 build the new ones that we are going to need. 07:53.400 --> 08:02.410 So first of all we need to import the new classes so I can in fact copy this one as well. 08:02.530 --> 08:07.590 So but this time we don't put it in our classes. 08:07.780 --> 08:19.770 So one detail API if view here and at least create API view perfect. 08:20.040 --> 08:24.180 We can now connect them to the appropriate end points. 08:24.230 --> 08:37.180 We need to use as view seeing think for create a view that s view and you can also change the indentation 08:37.290 --> 08:39.340 to beat like so what. 08:46.840 --> 08:53.370 And to perfect now the starting points that are completed we can around the development server and check 08:53.460 --> 08:55.390 out a work. 08:55.920 --> 08:56.990 So let's go back. 08:58.000 --> 09:00.420 To Chrome and let's check. 09:00.410 --> 09:03.680 D first and point. 09:03.680 --> 09:06.110 Everything works as expected clearly. 09:06.290 --> 09:10.070 It's great then grab the details of a specific instance. 09:10.250 --> 09:13.890 Perfect and maybe we can also create a new one. 09:13.970 --> 09:18.800 I'm going to go to the Lesh API select articles. 09:19.110 --> 09:23.290 I'm going to delete create that and update that. 09:23.450 --> 09:30.520 Same thing for the primary key second Article V arrest API 09:34.710 --> 09:38.710 post and edit is perfect everything works as expected. 09:39.390 --> 09:39.740 OK. 09:39.750 --> 09:44.330 That was it for this lecture about the API view class in the next lecture. 09:44.340 --> 09:49.440 We're going to start talking about a very important topic validation in January's framework.