WEBVTT 00:02.670 --> 00:09.420 In this lesson we are going to be the first and point of our advanced API this point will provide us 00:09.450 --> 00:14.960 some really useful information about the user that is currently connected to the application and we 00:14.970 --> 00:20.580 are going to use that information later on while developing the front end of the application to improve 00:20.670 --> 00:21.980 the user experience. 00:22.080 --> 00:27.310 Before doing that however it is crucial to talk about a very important aspect of application. 00:27.450 --> 00:34.820 I'm talking about this single page that is going to be rendered after the user as logged in. 00:34.830 --> 00:40.560 Basically I'm talking about the template what we're going to mount are a View application in the settings 00:40.590 --> 00:41.820 that by filing. 00:42.090 --> 00:48.030 We've set our briefing so that after logging the user is automatically redirected to the home page of 00:48.030 --> 00:48.900 our Web site. 00:48.900 --> 00:55.710 Therefore we now need to create a real path that points to the home page to these addresses and to this 00:55.710 --> 01:01.590 path we're going to link a view that is going to render the template the way we're going to mount our 01:01.590 --> 01:03.060 application. 01:03.060 --> 01:14.070 Let's create a folder called core and inside of this folder we can create it views dot by file and considering 01:14.070 --> 01:19.470 that we want to tell Django that we're going to use this core folder as a package inside of the folder 01:19.470 --> 01:20.770 we can create a done deal. 01:20.790 --> 01:27.510 You need a file and this is enough to tell Django and by phone that you are using this folder as a package 01:27.770 --> 01:29.730 and yet we can now create a review. 01:29.760 --> 01:32.180 Clearly we need to import some classes. 01:32.280 --> 01:38.070 So first of all from Django the country but not author. 01:38.210 --> 01:41.120 That makes sense. 01:41.280 --> 01:44.180 Import logging required mixed Cena. 01:44.350 --> 01:53.650 And from Django dot views dot generic dot BS import template view. 01:53.820 --> 02:02.580 We can create our class so class index template view which is going to extend into quite MC scene and 02:03.570 --> 02:09.260 template view and in case you're wondering why we're using the logo required. 02:09.450 --> 02:11.520 Well that's pretty easy to understand. 02:11.640 --> 02:17.130 As a matter of fact we're going to set out a breast API so that only authenticated users will be able 02:17.130 --> 02:22.560 to interact with the application and therefore it simply doesn't make sense to show them an empty page. 02:22.650 --> 02:28.370 Plus this way they're going to be redirected automatically to the logging forum to define which template 02:28.390 --> 02:29.640 name we want to use. 02:29.670 --> 02:36.800 We can just override the get template names matter which is going to accept the self as always. 02:36.820 --> 02:39.130 And we can define the template. 02:39.450 --> 02:45.770 Name equals index dot HMO and we can just return template name. 02:45.810 --> 02:51.690 There are of course easier ways to define which template we want to render inside a template view class. 02:51.690 --> 02:57.030 However the writing to get template names mapped out is going to be really useful later on. 02:57.030 --> 03:05.640 For now let's create these index dot e-mail file in the templates folder and inside of these templates 03:05.670 --> 03:08.520 we can create all the boilerplate code we need. 03:08.520 --> 03:09.460 This can be. 03:09.870 --> 03:14.840 Question Time and yet inside we can maybe defining H1. 03:15.020 --> 03:20.660 And yeah we can just write the yes and the down below we can define a deal. 03:20.710 --> 03:25.020 We've tidied up so as you see this is very similar. 03:25.020 --> 03:30.210 Basically the same that we've done while developing a few applications in that view. 03:30.330 --> 03:31.200 Yes section. 03:31.470 --> 03:37.900 We now need of course to create the path for our index template view and we need to import it inside 03:38.250 --> 03:47.790 the US note by file so here or maybe even appear from cardboard views import index template view and 03:47.790 --> 03:55.770 we also need to import the red parser function and dysfunction allows us to create parts for our views 03:55.830 --> 03:57.850 based on irregular expressions. 03:57.870 --> 04:03.090 Basically the same data we used to do with the older versions of Django and that's because we want to 04:03.090 --> 04:07.260 redirect every request to these index template view. 04:07.260 --> 04:12.590 This is going to be crucial really really important while developing the front end of the application 04:12.960 --> 04:16.350 and in order to not mess with all the other parts of course. 04:16.350 --> 04:23.760 And at the point that we have defined we can just create the path here at the end of the URL but at 04:23.940 --> 04:34.020 least so let's create a catch all the regular expression and then yeah we can call index template view 04:34.160 --> 04:41.570 dot s view and as a name we can set entry point. 04:41.570 --> 04:48.140 Let's not test this new path let's run Randi development server and let's move to Chrome and then you 04:48.140 --> 04:49.840 can see are we read view. 04:49.840 --> 04:57.710 J Yes because this is the page we've added the user we've I.D. up and we seed of course because we authenticated. 04:57.710 --> 05:07.150 So let's go to I mean let's log out we are redirected to the logging form of course let's try to call 05:07.150 --> 05:11.260 you on page and then you can see of course we are ready to act it to the logging forum. 05:11.750 --> 05:15.830 Let's not focus on creating the end point we were talking about at the beginning of the lesson. 05:15.860 --> 05:23.720 So for now we can close all of these files like so and inside of the users folder I'm going to create 05:23.720 --> 05:37.270 another folder API and instead of API I'm going to create see that ISIS dot by then views goodbye and 05:38.600 --> 05:39.920 yes goodbye. 05:40.760 --> 05:46.730 Let's start by creating the realize that class is we've already said this end point we provide us information 05:46.730 --> 05:51.920 about the user that is currently connected to the application and specifically it's going to provide 05:51.920 --> 05:59.330 us each user name therefore we can create this utilize that to serialize this field from rest framework 05:59.950 --> 06:08.560 import that ISIS and front users dot model import custom use that. 06:08.780 --> 06:14.290 Now we can create plus a user display. 06:14.280 --> 06:15.020 See the riser. 06:15.470 --> 06:19.840 And this is of course models and Atlas is going to extend. 06:19.840 --> 06:22.990 She realizes that model see eyes. 06:23.450 --> 06:26.040 Let's now define class matter. 06:26.220 --> 06:33.680 We model equals custom user and fields equals user name. 06:33.740 --> 06:35.280 Let's not create a view to use. 06:35.290 --> 06:36.620 We have these satellites. 06:36.890 --> 06:42.290 And of course Django framework provides us several classes that we can use to build such a view. 06:42.290 --> 06:46.540 And I believe that in this case the EPA view class itself is a great choice. 06:46.550 --> 06:59.420 So from blessed framework not a response in fourth response and from rest framework dot vs import API 06:59.420 --> 07:06.350 view and then from users that API dot utilizes important user this place utilize it. 07:06.820 --> 07:16.010 And now here plus the current user API viewer which is going to extend the API Europe. 07:16.190 --> 07:23.930 And here we can define the get method which accepts self and request to let's define that either as 07:24.830 --> 07:30.130 user display that either entities finally want to pass the request but use it. 07:30.620 --> 07:38.930 We can now return a response and there's a response we want to return serialize that not data. 07:38.930 --> 07:41.210 Let's create the endpoint itself. 07:41.450 --> 07:49.890 So from Django don't you or else import parser and from users not API dot views. 07:50.090 --> 07:54.430 Import user display API. 07:54.590 --> 07:55.790 View now. 07:56.150 --> 08:02.810 Yeah you get out Panthers equals the list in the bathroom. 08:03.420 --> 08:09.340 Users or even just user which is going to call user display. 08:09.460 --> 08:17.770 He gave you dot s view and as a name for the end point we said current user. 08:18.020 --> 08:23.780 Now considering that decent point will provide us the user name of the user that is currently connected 08:23.810 --> 08:28.520 to an application we need to be sure to only set of authenticated requests. 08:28.610 --> 08:33.680 And considering that as we've said several times you want all the authenticated users to interact with 08:33.680 --> 08:37.550 that application we can't just set the permission glasses and globally. 08:37.550 --> 08:47.830 So yeah in the rest framework dictionary I can call it these key like so but D is of course going to 08:47.830 --> 09:05.380 be default admission classes and I can just define a framework that shows that is authenticated because 09:05.410 --> 09:12.070 I remember you that by default we have allow any let's now move to the understood by file of the project 09:12.100 --> 09:17.300 because of course we need to include d you are registered by file. 09:17.740 --> 09:22.270 So yeah I'm just going to copy this park basted like so. 09:22.800 --> 09:30.160 So this is going to be API and it's going to include users not API not us. 09:30.910 --> 09:37.240 That's all random development server and let's test the new endpoint we get this added data because 09:38.020 --> 09:45.590 the proper name is current use it API view so 1 and so yeah. 09:45.610 --> 09:48.480 So let's restart the development side of it once again. 09:50.360 --> 09:58.700 Let's now move to Cairo where we can go to slash EPA slash use that we get the message authentication 09:58.700 --> 10:01.110 credentials were not provided very good. 10:01.460 --> 10:04.350 So let's look in under 10:07.760 --> 10:10.300 and of course we get our user name. 10:10.310 --> 10:11.000 As expected.