WEBVTT 00:00.090 --> 00:01.680 Hello everyone and welcome back. 00:01.680 --> 00:04.870 In this lecture we're going to give an introduction to Django this framework. 00:04.890 --> 00:09.900 And we're going to set up the news API project that we're going to use throughout the whole section. 00:10.020 --> 00:17.550 So what to ease Django rest framework Django this framework is a powerful and flexible toolkit for building 00:17.580 --> 00:22.600 where BPI is with Python and Django we can install it via peep. 00:22.620 --> 00:30.390 As for any other Python package and included in any existing Django project just by adding best framework 00:30.570 --> 00:37.220 to things styled ops least Django this framework is therefore not a modified version of Django. 00:37.260 --> 00:44.220 As somebody might think it's a package that we treat as any are there Django application that we need 00:44.220 --> 00:51.450 to include into our project and the goal of the package is to provide us a powerful tool kit for building 00:51.510 --> 00:58.260 web API as Django framework allows us to create the power of full production ready web API is in a very 00:58.260 --> 00:59.680 short time. 00:59.820 --> 01:05.760 We want the support and advantages of the Python and Django ecosystems and communities. 01:05.760 --> 01:08.280 It's the number one package for creating a REST API. 01:08.280 --> 01:15.630 We've Django used by world class companies such as Mozilla Heroku redhead and Eventbrite Django framework 01:15.660 --> 01:21.120 intentionally uses many of jingles programming conventions making it a natural extension of the package 01:21.390 --> 01:23.550 that is easy to pick up and understand. 01:23.670 --> 01:28.230 In this lesson we're going to create a new Django project that we're then going to use it throughout 01:28.260 --> 01:34.260 the whole section as we said learning how to use all of Django frameworks main components by creating 01:34.260 --> 01:36.160 a web API free news Web site. 01:36.360 --> 01:42.570 Let's get started as you can see I have not opened yet Visual Studio code. 01:42.630 --> 01:46.920 What you see is an instance of Linux terminal. 01:46.980 --> 01:54.090 I'm currently in the jungle as framework level one folder of the repository and this is how why you 01:54.090 --> 01:57.600 would normally create a new Django project. 01:57.600 --> 02:01.560 I would start from creating a new virtual environment. 02:01.560 --> 02:11.540 I'm going to use that the end module and I can just call it VM I'm then going to activate the environment 02:11.600 --> 02:19.700 by giving source VM being activate and considering that as we've said Django this framework is a package 02:19.730 --> 02:23.510 that we need to include within our Django project. 02:23.510 --> 02:30.140 It means that in order to create a web BPI using django as framework in Django we need to install both 02:30.140 --> 02:37.220 packages within our physical environment so people install Django 02:40.020 --> 02:49.380 and we can install Django this framework by giving people install Django framework no spaces or IFAs 02:53.120 --> 03:04.870 so I'm now going to create a new Django project Django admin start project use API. 03:05.310 --> 03:10.680 So we've seen the Django this framework level one before that we've now got our feet wet environment 03:10.740 --> 03:13.530 and out and use API project. 03:13.980 --> 03:22.370 I'm now going to move change directory within News API which is our project and the edit within these 03:22.370 --> 03:29.890 four that I'm now going to open these ones to your code giving the command code Dot this is going to 03:29.890 --> 03:37.210 be very helpful in several different ways and allows us to get a clear picture of the whole project 03:37.270 --> 03:40.880 3 without any other fights getting in the way. 03:40.900 --> 03:45.490 So the first thing we need to do is to select the Python interpreter. 03:45.530 --> 03:49.760 OK we're going to use the shortcut show all commands. 03:50.240 --> 03:58.030 Then select interpreter and inside the list with all the available interpreters is also available as 03:58.030 --> 04:03.450 you can see the one we've just created within the Jenkins framework level one folder. 04:03.610 --> 04:13.120 We can select it and what we can also do is to go to file Preferences Settings workspace settings. 04:13.360 --> 04:15.470 You can then look for VM. 04:16.090 --> 04:21.520 In the we can specify your path to folder we have a list of V12 environments. 04:21.910 --> 04:26.500 So in the case of this project the Django this framework level one folder. 04:26.740 --> 04:32.050 So I'm going to go back to the terminal right here least. 04:32.240 --> 04:33.760 It is our V12 environment. 04:33.760 --> 04:37.480 So print working directory. 04:37.510 --> 04:43.990 I'm just going to specify the path to the folder. 04:44.580 --> 04:45.320 Ok. 04:45.330 --> 04:53.820 We can find it in settings that Jason this option allows everybody to create their own V12 environment 04:54.330 --> 05:01.320 give into the project a bit more of consistency and furthermore it's really helpful if for any reason 05:01.360 --> 05:01.810 visible. 05:01.890 --> 05:06.740 Your code can't manage to find the visual environment that you've created. 05:06.960 --> 05:14.850 So we can now close setting switches on and set things we can instead open up a new terminal and we 05:14.850 --> 05:18.870 can now create a new application for our project. 05:19.290 --> 05:28.770 So Python managed that by start up we can just call it new zoom and we can now go ahead and install 05:29.500 --> 05:38.410 the new applications so from within the settings not by file we can go to that installed apps list. 05:39.360 --> 05:48.270 And we can add our application news and within the installed apps we also need to install Django framework 05:48.570 --> 05:49.270 in fact. 05:49.470 --> 05:53.070 So rest framework 05:56.480 --> 06:03.320 and now it's done with plugging it in it's ready to be used and by the way if you're wondering which 06:03.860 --> 06:10.110 team amusing for the workbench I'm currently using one Molokai team. 06:10.170 --> 06:15.210 So now that we've created the news app it's time to finally create our models. 06:15.900 --> 06:22.850 So let's create an article model classic article it extends. 06:22.980 --> 06:27.560 Clearly models that model as always. 06:27.690 --> 06:34.800 And the article model is going to have the wind fields first of all an author which we can define as 06:34.810 --> 06:36.450 Chatfield for now. 06:36.450 --> 06:41.560 So models dot Chad field we've max length. 06:41.580 --> 06:56.150 We can set it to 50 Dan title models Dot charter field and this time we can set max length to 120 then 06:56.160 --> 07:07.380 description models dot Chatfield and we can set Max linked to two hundred. 07:07.580 --> 07:09.400 Then we're going to need a body. 07:09.710 --> 07:19.310 So models dot text field and location models still Chad field. 07:19.520 --> 07:24.020 And we can use in effect one hundred and twenty. 07:24.260 --> 07:32.480 So I'm just going to copy this code then the model is going to have a publication date so a publication 07:33.620 --> 07:39.980 date models start date field. 07:40.090 --> 07:45.830 Does anybody in flag active models dot William field. 07:45.830 --> 07:51.150 We default through and then we went to the timestamps. 07:51.170 --> 08:02.050 So created that which is going to be model start date time field a daytime field. 08:02.080 --> 08:12.010 We've auto now ad equals through and we want to do something similar for another field. 08:12.010 --> 08:20.330 The updated at field which is going to have output now through. 08:20.340 --> 08:27.210 So the difference between our now ad and out now is of course that we've created ad to the field is 08:27.210 --> 08:29.010 going to be set automatically. 08:29.070 --> 08:37.200 Once a new instance of a model it's created while using also now through allows the field to be out 08:37.200 --> 08:44.420 traumatically set each time a model instance is saved making it perfect of course for an updated at 08:44.430 --> 08:45.120 field. 08:45.120 --> 08:50.400 Then of course we need to define then the string on that string. 08:51.750 --> 09:07.050 But we're going to return an F string so self that author space and then self-taught title perfect. 09:07.060 --> 09:14.800 Now that the model is completed it's time to give their make migrations command so Python managed by 09:16.000 --> 09:25.890 make migrations and yada yada and we can now give the command migrate so Python manage that by migrate 09:28.170 --> 09:28.940 now that we're here. 09:28.950 --> 09:31.190 Let's also create our super user. 09:31.200 --> 09:37.180 So Python managed that by create super use it. 09:39.360 --> 09:44.150 I can call it admin no email address 09:48.220 --> 09:55.540 and no way to basically ready for creating new instances we need of course to register our model within 09:55.540 --> 09:57.000 the admin dot by 5. 09:57.040 --> 10:06.580 So I'm going to imported first from news DOD's models import article and here I can do. 10:06.610 --> 10:14.290 Add me on that site to register and I can register the article model like so. 10:15.310 --> 10:18.840 So let's go ahead and execute our development server. 10:19.060 --> 10:26.290 I'm going to use the debug window so first of all we need to set the configuration it's currently set 10:26.290 --> 10:27.970 up for python. 10:27.970 --> 10:28.720 Current file. 10:28.720 --> 10:36.070 We can go ahead and specify Python Django and one thing that it might be really important to set is 10:36.070 --> 10:37.550 to comment out there. 10:37.570 --> 10:46.390 Not threading argument from the Django configuration Jason you can comment this out or just delete it 10:46.720 --> 10:47.760 altogether. 10:47.770 --> 10:53.500 If you're using chrome and you're finding the development server to be slow so we're not ready. 10:53.500 --> 11:00.840 Let's go ahead and run our development server and either we go as server his life system check it into 11:00.840 --> 11:07.640 fight no issues perfect and to complete the setup of our project we can now go to Chrome and create 11:07.640 --> 11:11.260 a first articulate instance from Django as admin. 11:11.540 --> 11:18.680 So let's switch to Chrome and we've got our success message of course the install work successfully 11:18.690 --> 11:19.580 congratulations. 11:19.580 --> 11:29.780 So let's go to slash admin admin credentials ADR and use up with the article model. 11:29.870 --> 11:38.660 So let's add in your article in the offer field we can just add John Doe for now and maybe we can create 11:38.660 --> 11:51.670 the scientific articles so happy birthday SS 20 years of the International Space Station. 11:52.030 --> 12:04.880 Yeah we can just add some fancy description and adjust some content location Earth date today. 12:04.880 --> 12:10.910 Boolean flag said is true by default and debt the fields of course get set automatically by Django. 12:11.000 --> 12:12.600 So let's save. 12:12.980 --> 12:13.460 Perfect. 12:13.460 --> 12:19.040 Now that our project is set up and now that we've created the first instance of article we're not ready 12:19.040 --> 12:25.120 to start talking about one of the fundamental components of Django as frameworks architecture. 12:25.130 --> 12:30.400 And I'm talking about serialize this and we're going to discuss our work in the next lecture. 12:30.410 --> 12:30.920 See you there.