WEBVTT 00:00.120 --> 00:04.090 Hello everyone and welcome to this new lesson about the EPA view the. 00:04.230 --> 00:09.870 Now that we've learned how to use it allows us to serialize and this serialized data it's finally time 00:09.870 --> 00:16.230 to write our first EPA view to use them with January's framework provides two rappers we can use to 00:16.230 --> 00:18.030 write EPA views. 00:18.030 --> 00:24.110 One of these is the EPA view decorator for working with function based EPA views. 00:24.120 --> 00:31.920 The other one is the EPA view class for working with class based EPA views because of this wrappers 00:32.160 --> 00:33.690 out of use we get for free. 00:33.690 --> 00:40.350 All the code that is necessary to receive requested instances provide an appropriate response and handle 00:40.350 --> 00:46.950 exceptions such as the pass air or exception that occurs when access request does data with malformed 00:46.950 --> 00:47.980 input. 00:48.030 --> 00:51.810 In this lesson we're going to create two EPA views that will provide. 00:51.810 --> 00:56.570 Read their right functionalities for other articles using the EPA view decorator. 00:56.580 --> 01:01.180 We're also going to introduce one of January's frameworks most cherished and useful features. 01:01.230 --> 01:02.370 The browser will API. 01:02.460 --> 01:04.070 Let's get started. 01:04.180 --> 01:06.550 Okay so here we are invisible to your code. 01:06.570 --> 01:12.720 And the first thing that we want to do is to create the views that Pi Phi within the API folder we've 01:12.720 --> 01:19.250 created in the previous lesson so that we have a specific file to create our API views within. 01:19.560 --> 01:25.370 We could use the views that Pi Phi that's been automatically created by Django when we've created the 01:25.370 --> 01:26.490 news app. 01:26.890 --> 01:33.840 However creating all the files that we need within the API folder is a much better choice considering 01:33.840 --> 01:39.900 that it allows us to keep a much more consistent structure within our project. 01:39.900 --> 01:50.370 So new file views that by we can close models that by and see that either stood by for now and the first 01:50.370 --> 01:56.220 thing that we want to do of course is to make all the imports that we're going to need to create our 01:56.310 --> 01:57.660 API views. 01:57.690 --> 02:00.260 So first of all we're going to need starters. 02:00.420 --> 02:08.680 So from this framework import status then we're going to need DARPA view decorator. 02:08.700 --> 02:18.720 So from this framework dot decorators input API view then we want to import a response. 02:18.870 --> 02:23.240 So from this framework that response. 02:23.640 --> 02:30.600 Import response and then clearly we're going to need to import also our article more than in the article 02:30.660 --> 02:32.680 serialize that class. 02:32.700 --> 02:47.330 So from news that models import article and from use do the API dot serialize this import article 02:50.460 --> 02:51.260 serialize it. 02:52.590 --> 02:59.120 So we will really said that in this lesson we going to create two API views that will provide read and 02:59.120 --> 03:02.640 write functionalities for our articles model. 03:03.180 --> 03:09.070 And I think we can start by creating day least create EPA view for that article. 03:09.090 --> 03:19.260 So the effort is going to define the function article at least create API view which is going to accept 03:20.240 --> 03:24.790 requests for all the function based views. 03:24.860 --> 03:32.180 So the first legit question you might be asking yourself is how can our view decide which kind of functionality 03:32.180 --> 03:33.330 to provide. 03:33.350 --> 03:41.090 I mean how does it know that it has to create a new instance of article or it has to show a list with 03:41.150 --> 03:43.530 all the articles in our database. 03:43.730 --> 03:49.670 And the answer is that is going to know that based on the kind of request that is being called by. 03:49.850 --> 03:57.050 Remember that we said that in the rest architecture a Rest Api provides different kind of functionalities 03:57.380 --> 04:05.480 based on the request that the client is making to the API and we actually have to define the kind of 04:05.870 --> 04:10.280 functionality that our view will be able to manage passing. 04:10.280 --> 04:19.040 At least we have dead different kinds of requests to the API view decorator and we do it like so. 04:19.460 --> 04:28.470 So we decorate are a function API view and we pass the list with the methods. 04:28.520 --> 04:40.000 So let's start by passing just to get method we can all check if request that method equals get it. 04:40.530 --> 04:42.750 And in this case we can act accordingly. 04:42.770 --> 04:48.320 So articles we get a query said we've all the articles that we want to show. 04:48.440 --> 04:51.910 In this case all the articles that have been flagged as active. 04:51.980 --> 05:03.980 So article objects dot filter active equals through and we can now feed adequately set to the articles 05:04.010 --> 05:08.050 serialized in order to get the data from the serialized it. 05:08.180 --> 05:10.610 Then we can return as a response. 05:10.940 --> 05:16.650 So serialize it equals articles utilize it. 05:17.740 --> 05:20.240 And we can feed it adequate he said. 05:21.020 --> 05:23.160 What do you think is this going to be enough. 05:23.270 --> 05:26.990 We'll pass in a query set to our article see realize it. 05:27.440 --> 05:29.030 Let's see what happens. 05:29.100 --> 05:32.750 Return our response. 05:32.780 --> 05:37.420 We said that we want to return to realize that not data. 05:37.420 --> 05:41.980 I need to fix this multiple year its response not risk pose. 05:42.200 --> 05:48.260 And we now need to connect our article least create EPA view to a U.S. rail path. 05:48.260 --> 05:56.770 Basically we need to create our first API and point so I'm going to create you arrested by within the 05:56.840 --> 06:06.620 API folder and yet I can import path and so from Django don't you or else import path and you also need 06:06.620 --> 06:09.370 to import article least create API view. 06:09.740 --> 06:16.190 So from use the API that views import 06:19.990 --> 06:23.060 out of your function we can now create. 06:23.070 --> 06:35.250 Are you allowed partners list so path and we want to pass the End Point articles and so on articles 06:35.310 --> 06:43.820 we're going to provide both read and write functionalities for our articles. 06:43.970 --> 06:47.970 So article least we can just call it article list. 06:48.050 --> 06:54.140 We now need to connect the file to the main you had stood by file. 06:54.290 --> 06:58.500 So I'm going to force include. 06:58.710 --> 07:01.980 And here we can do API. 07:02.130 --> 07:11.910 So on the API path we want to include when to include use dot API not to address. 07:12.030 --> 07:14.100 We can now run our development server 07:17.090 --> 07:22.820 and now that it's up and running so we can go to Chrome and test our end point so clearly we've got 07:22.820 --> 07:24.960 no home page let's say. 07:24.960 --> 07:25.730 So yeah we get. 07:25.730 --> 07:33.620 Page not found a forum for error message but we've see clearly that we've got an API endpoint. 07:33.650 --> 07:36.510 So let's go through Lesh API. 07:37.610 --> 07:47.220 Articles you'll see we get an error attribute editor at Slate API Lesh articles got attribute error 07:47.490 --> 07:53.240 when attempting to get a value for field author on single eyes that article see that I said this you 07:53.270 --> 07:59.610 realize that a field might be name incorrectly and not match any attribute or key on date where he set 08:00.150 --> 08:01.590 instance. 08:01.590 --> 08:08.160 So that's the point we're feeling that way is set to are CDI is that therefore we get an error. 08:08.160 --> 08:13.170 What do we need to do to fix this error is to pass another parameter to want to see the light is it 08:13.680 --> 08:22.130 which is mainly equals through so that Aristide allies it now knows that he needs to serve a whole query 08:22.160 --> 08:31.680 set so it lets reloads development server let's go back to Rome and let's make another in fact get a 08:31.690 --> 08:39.550 request to our articles and point and E.D. adequately set at least we have the two articles that are 08:39.640 --> 08:41.800 available in our database. 08:41.800 --> 08:49.690 And as you can see content type application Jason what you are seeing here is Django frameworks browser 08:49.700 --> 08:51.010 able API. 08:51.160 --> 08:57.100 And it's basically a web interface that's used for development purposes which is you can clearly see 08:57.100 --> 09:04.780 it's very comfortable for us developers we see which kind of request we're making on which end point 09:04.870 --> 09:07.500 we get a lot of information on the request itself. 09:07.510 --> 09:09.530 You see we got the status quo 200. 09:09.550 --> 09:10.590 OK. 09:10.600 --> 09:15.070 The kind of methods that are allowed on these specific end point. 09:15.350 --> 09:20.940 And as you can see the Jason has been formatted so that is now ready for human consumption. 09:20.950 --> 09:27.940 Let's say we can also get the standard Jason format. 09:27.940 --> 09:34.720 But in general there won't be interface is the interface that will want to use most of our times. 09:34.720 --> 09:42.270 And it's important to note that such a browser API is not available in all web frameworks at all. 09:42.280 --> 09:47.710 Typically you would have to use a program such as postmen to communicate with your API. 09:47.830 --> 09:50.830 But as you can see luckily we've January's framework. 09:50.830 --> 09:52.460 It is not needed. 09:52.510 --> 09:58.550 We are where we are going to use postman in one specific lesson later on during the course. 09:58.600 --> 10:03.840 But just to make an example we're going to use the browser API from now on. 10:03.850 --> 10:07.550 So let's now go back to visual studio code. 10:07.820 --> 10:13.480 And before we start writing the rest of the code I want to make you notice that here we have not used 10:13.500 --> 10:20.500 Jace on response as we did with the pure Django API we've built in the preview section here. 10:20.500 --> 10:25.570 Clearly we're using the response class provided by Jane addressed framework. 10:25.690 --> 10:33.010 And so the really cool thing about this response class is that as we can read peaking on its definition 10:33.430 --> 10:43.030 the response class is an H TTP response that allows its data to be rendered into arbitrary media types 10:43.330 --> 10:49.510 which basically means that the Django this framework is going to decide which kind of response is most 10:49.510 --> 10:53.140 appropriate based on the context of the request itself. 10:53.650 --> 11:05.140 So let's continue now leave request that method equals post in that case first of all we initialize 11:05.220 --> 11:14.740 R C realize that so serialize that equals article sterilizer to which we pass data equals request not 11:14.950 --> 11:21.760 data and be careful because the request object used by Django its framework is different from the one 11:21.760 --> 11:23.680 that it's used by Django. 11:23.680 --> 11:32.960 So we can now check if serialize the that is valid which as we've said is a check that we must perform. 11:33.100 --> 11:38.210 In this case we call C that I said don't save. 11:38.560 --> 11:45.260 Which is then going to call specific method based on the kind of request. 11:45.940 --> 11:51.530 And then once we called safe we could return a response. 11:51.560 --> 12:00.350 So return response and the response is going to contain C realize that data and statues equals start 12:00.360 --> 12:08.680 to start a GDP to a one created if C realize that dot is valid the returns false. 12:08.690 --> 12:22.780 Instead we do return response C realize that not errors and is a status code return status dot GDP 400 12:23.180 --> 12:25.250 by the request. 12:25.250 --> 12:29.800 So our view is basically ready to accept both get and post requests. 12:30.050 --> 12:36.790 We just need to include post within the list of accepted requests. 12:36.800 --> 12:39.840 We can also add one line of space here. 12:39.890 --> 12:49.370 Let's now restaurateur development server and let's go back to the browser API make another request 12:49.370 --> 12:57.830 to the same endpoint and we now see data also allows post requests and if we scroll down to page we 12:57.830 --> 13:06.800 see that we've now got a very convenient form that we can use to send data to our API basically to create 13:06.920 --> 13:08.500 new instances. 13:08.540 --> 13:10.340 So let's make a test. 13:10.340 --> 13:17.240 I'm going to copy this Jason and I'm going to remove created it and updated it. 13:18.200 --> 13:20.890 Which is we know that managed by Django. 13:21.140 --> 13:27.070 And I'm also going to remove a deep field like so I can also change the publication date. 13:27.080 --> 13:33.710 So let's say I want to I want this article to be published on the 25th and I'm going to change the title 13:35.020 --> 13:46.990 first article V addressed API I can click post to make a post request to d end point that we've defined 13:47.790 --> 13:50.900 in order to actually get a new instance. 13:50.980 --> 14:00.420 So let's write perfect success a EDP 2 or 1 created and yet we get back add a new instance. 14:00.490 --> 14:08.350 Now if we make a get request once again to the same endpoint we see now we've got 3 article instances 14:08.600 --> 14:09.460 perfect. 14:09.700 --> 14:17.320 And if we now go back to these well pseudocode yet indeterminate we see we got a little surprise see 14:17.320 --> 14:25.350 we got a dictionary we have the validated data for example publication date includes date time dot data 14:25.360 --> 14:32.730 you see and that's because remember India creates method within our article see that is it. 14:32.890 --> 14:37.060 I had a defined D print print and validated data. 14:37.060 --> 14:44.980 Therefore this print statement has been executed right before creating a new article is this that is 14:44.980 --> 14:46.360 then been returned.