WEBVTT 00:03.530 --> 00:08.720 So let's now focus on building a second API view. 00:08.750 --> 00:19.430 So Def article detail API view which accepts a request and a paramedic key they were going to use to 00:19.430 --> 00:22.400 know which instance we want to work with. 00:22.580 --> 00:28.660 Now that we know how to use the API view decorator we can start by dictating our view. 00:28.760 --> 00:33.120 So API view we're going to pass a list. 00:33.120 --> 00:37.890 And so as we did for our article least create API view. 00:38.000 --> 00:46.440 This list is going to include all the ATP verbs that we want our API view to be able to manage. 00:46.460 --> 00:57.260 So in this case I'm going to just get put and delete because using these API view we want our users 00:57.260 --> 01:04.550 to be able to retrieve data about a specific instance or update the same data or delete these steps 01:04.610 --> 01:06.050 altogether. 01:06.140 --> 01:08.000 So let's start to write some code. 01:08.000 --> 01:14.630 First of all we want to know if the primary keyword is being passed via our End Point is actually valid 01:14.900 --> 01:21.320 which means that if it's valid our database is actually going to contain a record which will correspond 01:21.530 --> 01:24.170 to the primary key that is being passed. 01:24.170 --> 01:26.880 We could use get object or for a fourth. 01:26.880 --> 01:33.620 There is also a function that is provided by jingoist framework a specific version of Get object or 01:33.620 --> 01:34.230 for a fourth. 01:34.440 --> 01:41.270 However I believe it's better to write a couple more of lines in this specific case to actually line 01:41.270 --> 01:43.790 out the eventual error message. 01:43.790 --> 01:52.760 The way we wanted to be so we're going to use a try except clause to write article equals article dot 01:52.880 --> 02:06.090 objects that get with primary key equals primary key except article that does not exist. 02:06.380 --> 02:13.760 In that case we return a response and we define a dictionary with the error message basically that we 02:13.760 --> 02:26.430 want to show so error and then another dictionary which is going to include the code. 02:26.460 --> 02:28.760 Remember this is for human consumption. 02:28.790 --> 02:37.400 So code for a for message article not found. 02:37.400 --> 02:40.890 And that's enough for the error message. 02:40.930 --> 02:48.940 Maybe you can just unintended like so and we can help us started as a sort of status equal status dot 02:49.110 --> 02:53.430 HDP fault for not found. 02:53.440 --> 02:59.890 Now if the article exists instead we can start to check the kind of request that has been made and based 02:59.890 --> 03:02.230 on that provide an appropriate response. 03:02.250 --> 03:14.390 So if request that method equals get it then we get out to realize it and I said equals article utilize 03:14.480 --> 03:22.090 it and we feed our article to it and then we just return it. 03:22.190 --> 03:33.510 C realize that just data like so any request that method equals full term which means that a user wants 03:33.510 --> 03:38.190 to update a specific record we take us to that. 03:38.310 --> 03:39.250 So either. 03:39.360 --> 03:52.050 It was pretty close utilized but this time we also pass data equals request dot data we check if C realize 03:52.050 --> 03:55.800 that dot is valid. 03:55.800 --> 04:00.900 Remember we always need to check if cedar I's that dot is valid. 04:00.900 --> 04:07.830 And in that case we call C realize that dot save which is going to trigger the update method and that's 04:07.830 --> 04:19.950 why will surpass article and then we're going to return a response based on serialize the DOT data. 04:19.950 --> 04:29.940 Otherwise you serialize the return an invalid response basically false with no return address and starts 04:30.000 --> 04:41.400 a 400 bed request then Elif requests that method equals delete. 04:41.940 --> 04:45.890 In that case we want to call did delete method on the instance. 04:45.950 --> 04:49.480 So particle dot delete. 04:50.480 --> 04:52.350 And then we're going to return. 04:52.470 --> 04:54.260 Response. 04:54.440 --> 05:03.230 Status equals status dot age GDP 2 0 4 not content. 05:03.230 --> 05:03.740 Perfect. 05:03.740 --> 05:09.980 We have now ready to actually define an other end point. 05:10.130 --> 05:20.380 So first of all we need to import a new view like so and stay in alphabetical like would actually invert 05:20.380 --> 05:34.410 the position and maybe even jump a line like so I can now create the second end point so articles slash 05:35.000 --> 05:37.580 integer primary key. 05:38.060 --> 05:43.680 This time we want to call practical detail EPA view and the name is going to be critical detail. 05:43.700 --> 05:52.470 Of course very well we can now restart our development server and let's go back to Cairo. 05:52.620 --> 05:58.190 So let's try and get the details of or maybe the second article. 05:58.240 --> 06:04.380 Okay so I'm going to go to API articles. 06:04.440 --> 06:10.440 The primary key to and let's see what we get back. 06:10.440 --> 06:18.200 Perfect as you can see we get the details of the article with primary key to we see dead DNA bind allows 06:18.210 --> 06:26.550 delete methods options get and put it and we can actually three good options just by clicking here on 06:26.550 --> 06:31.410 options and you see we get the details of the renders and passes. 06:31.410 --> 06:38.340 So what's this end point renders and what it passes and you see we get Jason but also HDMI all because 06:38.340 --> 06:39.830 of the browser API. 06:39.870 --> 06:48.720 Same thing for Jason X W W W for you et al encoded and most importantly form data because of the browser 06:48.720 --> 06:49.470 bool API. 06:49.860 --> 06:59.910 So let's try and update our instance we need to pass the J So like so I'm going to remove created it 07:00.630 --> 07:10.230 updated it and idea that I'm going to update maybe the body so some content updated like so. 07:10.230 --> 07:17.400 So now clicking on PWD we're going to make a put request towards our end point possibly these data telling 07:17.400 --> 07:23.130 DC that I there basically that we want to update a specific record we've the data we've just passed. 07:23.130 --> 07:26.210 So let's try it put and if we get it. 07:26.220 --> 07:33.390 You see everybody has been updated and if we now go and make a get request to the basic end point API 07:33.390 --> 07:34.480 is less articles. 07:34.590 --> 07:38.630 We see that now the record with primary key number 2 has been updated. 07:38.640 --> 07:39.770 Perfect. 07:40.050 --> 07:41.730 We can all make our last test. 07:41.740 --> 07:45.270 Let's go to the same endpoint with the same primary key. 07:45.340 --> 07:51.160 Let's make it delete request towards the same end endpoint so delete it. 07:51.180 --> 07:54.320 Are you sure you want to delete this article detailed API. 07:54.340 --> 08:01.320 You basically using the name of our view and yes we're sure we want to delete this specific instance 08:01.890 --> 08:10.140 deletion completed we get back HBP 2 or for no content perfect and if we now go to our list and point 08:10.500 --> 08:16.450 we see now we've got only two instances the one we've primary key one and the one we've primary key 08:16.670 --> 08:17.490 3. 08:17.610 --> 08:19.020 Perfect. 08:19.020 --> 08:26.310 In fact we can also make a very last to test it going and trying to grab the details of an article that 08:26.310 --> 08:27.300 doesn't exist. 08:27.300 --> 08:33.540 For example one way for primary key for how you see we get or four not found. 08:33.540 --> 08:36.730 We've added our code photo for our message. 08:36.900 --> 08:37.930 How do think are not found. 08:37.970 --> 08:40.230 Everything works as expected. 08:40.290 --> 08:40.980 Okay perfect. 08:41.020 --> 08:45.670 That was it for this lesson about the view decorator in the next lesson. 08:45.690 --> 08:51.540 We're going to see how to use the API view class to create class based EPA views. 08:51.540 --> 08:52.680 We've Django this framework.