Hello everyone and welcome back.
In this lecture we're going to give an introduction to Django this framework.
And we're going to set up the news API project that we're going to use throughout the whole section.
So what to ease Django rest framework Django this framework is a powerful and flexible toolkit for building
where BPI is with Python and Django we can install it via peep.
As for any other Python package and included in any existing Django project just by adding best framework
to things styled ops least Django this framework is therefore not a modified version of Django.
As somebody might think it's a package that we treat as any are there Django application that we need
to include into our project and the goal of the package is to provide us a powerful tool kit for building
web API as Django framework allows us to create the power of full production ready web API is in a very
short time.
We want the support and advantages of the Python and Django ecosystems and communities.
It's the number one package for creating a REST API.
We've Django used by world class companies such as Mozilla Heroku redhead and Eventbrite Django framework
intentionally uses many of jingles programming conventions making it a natural extension of the package
that is easy to pick up and understand.
In this lesson we're going to create a new Django project that we're then going to use it throughout
the whole section as we said learning how to use all of Django frameworks main components by creating
a web API free news Web site.
Let's get started as you can see I have not opened yet Visual Studio code.
What you see is an instance of Linux terminal.
I'm currently in the jungle as framework level one folder of the repository and this is how why you
would normally create a new Django project.
I would start from creating a new virtual environment.
I'm going to use that the end module and I can just call it VM I'm then going to activate the environment
by giving source VM being activate and considering that as we've said Django this framework is a package
that we need to include within our Django project.
It means that in order to create a web BPI using django as framework in Django we need to install both
packages within our physical environment so people install Django
and we can install Django this framework by giving people install Django framework no spaces or IFAs
so I'm now going to create a new Django project Django admin start project use API.
So we've seen the Django this framework level one before that we've now got our feet wet environment
and out and use API project.
I'm now going to move change directory within News API which is our project and the edit within these
four that I'm now going to open these ones to your code giving the command code Dot this is going to
be very helpful in several different ways and allows us to get a clear picture of the whole project
3 without any other fights getting in the way.
So the first thing we need to do is to select the Python interpreter.
OK we're going to use the shortcut show all commands.
Then select interpreter and inside the list with all the available interpreters is also available as
you can see the one we've just created within the Jenkins framework level one folder.
We can select it and what we can also do is to go to file Preferences Settings workspace settings.
You can then look for VM.
In the we can specify your path to folder we have a list of V12 environments.
So in the case of this project the Django this framework level one folder.
So I'm going to go back to the terminal right here least.
It is our V12 environment.
So print working directory.
I'm just going to specify the path to the folder.
Ok.
We can find it in settings that Jason this option allows everybody to create their own V12 environment
give into the project a bit more of consistency and furthermore it's really helpful if for any reason
visible.
Your code can't manage to find the visual environment that you've created.
So we can now close setting switches on and set things we can instead open up a new terminal and we
can now create a new application for our project.
So Python managed that by start up we can just call it new zoom and we can now go ahead and install
the new applications so from within the settings not by file we can go to that installed apps list.
And we can add our application news and within the installed apps we also need to install Django framework
in fact.
So rest framework
and now it's done with plugging it in it's ready to be used and by the way if you're wondering which
team amusing for the workbench I'm currently using one Molokai team.
So now that we've created the news app it's time to finally create our models.
So let's create an article model classic article it extends.
Clearly models that model as always.
And the article model is going to have the wind fields first of all an author which we can define as
Chatfield for now.
So models dot Chad field we've max length.
We can set it to 50 Dan title models Dot charter field and this time we can set max length to 120 then
description models dot Chatfield and we can set Max linked to two hundred.
Then we're going to need a body.
So models dot text field and location models still Chad field.
And we can use in effect one hundred and twenty.
So I'm just going to copy this code then the model is going to have a publication date so a publication
date models start date field.
Does anybody in flag active models dot William field.
We default through and then we went to the timestamps.
So created that which is going to be model start date time field a daytime field.
We've auto now ad equals through and we want to do something similar for another field.
The updated at field which is going to have output now through.
So the difference between our now ad and out now is of course that we've created ad to the field is
going to be set automatically.
Once a new instance of a model it's created while using also now through allows the field to be out
traumatically set each time a model instance is saved making it perfect of course for an updated at
field.
Then of course we need to define then the string on that string.
But we're going to return an F string so self that author space and then self-taught title perfect.
Now that the model is completed it's time to give their make migrations command so Python managed by
make migrations and yada yada and we can now give the command migrate so Python manage that by migrate
now that we're here.
Let's also create our super user.
So Python managed that by create super use it.
I can call it admin no email address
and no way to basically ready for creating new instances we need of course to register our model within
the admin dot by 5.
So I'm going to imported first from news DOD's models import article and here I can do.
Add me on that site to register and I can register the article model like so.
So let's go ahead and execute our development server.
I'm going to use the debug window so first of all we need to set the configuration it's currently set
up for python.
Current file.
We can go ahead and specify Python Django and one thing that it might be really important to set is
to comment out there.
Not threading argument from the Django configuration Jason you can comment this out or just delete it
altogether.
If you're using chrome and you're finding the development server to be slow so we're not ready.
Let's go ahead and run our development server and either we go as server his life system check it into
fight no issues perfect and to complete the setup of our project we can now go to Chrome and create
a first articulate instance from Django as admin.
So let's switch to Chrome and we've got our success message of course the install work successfully
congratulations.
So let's go to slash admin admin credentials ADR and use up with the article model.
So let's add in your article in the offer field we can just add John Doe for now and maybe we can create
the scientific articles so happy birthday SS 20 years of the International Space Station.
Yeah we can just add some fancy description and adjust some content location Earth date today.
Boolean flag said is true by default and debt the fields of course get set automatically by Django.
So let's save.
Perfect.
Now that our project is set up and now that we've created the first instance of article we're not ready
to start talking about one of the fundamental components of Django as frameworks architecture.
And I'm talking about serialize this and we're going to discuss our work in the next lecture.
See you there.