Hello everyone and welcome back.
In this lesson you're going to learn how to build a simple API using django that will expose the least
with all the products available in the database of a Web site which we're also going to create as the
title suggests.
The lesson is divided in two parts.
In this first part we're going to set up our Django project with models and configuration files making
a short recap of how to properly use Django as standard views to show our products in a HD email template.
In the second part of the lesson we will then proceed to the creation of the API itself by creating
the required DPA views and endpoints.
Writing a simple API without January's framework support will help us find out how incredibly useful
the package release understanding all the concepts explained so far in the course.
We have a completely practical example.
We will then be ready to start using django as framework in the next session.
Let's get started.
OK.
So we are adding these wells to your code and as you can see I'm currently weaving the same folder.
When I was developing the code for the previous lecture and this you can see I've created another folder
called the first API Django.
What I'm now going to create a new Django project.
Keep in mind however that this is not the only way clearly to develop with Django on a visa understood
your code.
Most of the times as we will see in the next sections you might want to open only the single folder
of your project.
However I believe that for this specific section of the course this is a good way to go.
Also considering that we will have to tweak the settings a little bit thus getting familiar with the
editor so I can now press the control shift B key combination together drop down menu with a list of
all the available commands and I can choose to use this virtual environment for the project by clicking
on Python.
Select interpreter Of course if for any reason this command is not on the top of the list you can just
look for it by typing its name you see I can now then select the command to activate the python extension
getting off course at least with all the available variable environments and interpreters.
So I'm now going to choose the one that I've created.
This one it asks me to install a limiter but I can just keep it for now.
One important thing to keep in mind is that if for any reason visible your code can't manage to find
the virtual environment that you want to use you can click on File Preferences Settings then on workspace
settings and you can look for VM over these two settings will pop up and you would be able to set the
path to the folder where you created the digital environments that you want to use.
But as you can see now everything works just fine.
And what I want to do instead is to install Django so I'm going to click on the original new terminal
a beautiful environment gets activated and I can now round instruction be install Django which is going
to download and install within that environment and the latest version of Django.
And we also need to install pillow deep install pillow which as you probably know is used by Django
to manage images.
So let's install it
and I can now just create a requirements that DST file making it easier for you to recreate the same
environment of course.
So Peter please requirements that tasty I'm not going to move within that first API Django folder and
I can now creates the project with the usual command.
So Django had me start a project and I can just call it online store.
We can now see of course that within the first API Django folder and Django project called online store
is being created.
We can now go ahead and run all the routine operation.
Let's call them.
So first of all Python managed that by my great now Python manager that by create super user and I can
just call it admin I can skip the email address is you can read them using a common password right now
but it's okay because we're developing remember that it's vital to use strong passwords in production
and the last thing that we can do before creating an app for our project is to set up visual studio
code to land that managed pi run server comment.
So I'm just going to debug tab and I can now click on the gear icon right here.
I'm going to select python which is going to open these launch door at the Jason File can now scroll
the file until we find this specific Jason.
And considering that Visual Studio code is opened within the web API e folder I have now to set the
new location of the manage those by file so I can add first API Django online store managed not by.
So going back to the back window I can now click on the drop down menu select Python Django to have
managed that by run server command run every time I press this green triangle and this is one of the
reasons why I really enjoy writing in Python web applications with VS Well your code.
This feature really makes the development process much easier.
And one last thing that you probably want to do if you're developing on Chrome as we are is to comment
that no threading option right here which otherwise might slow down the development server.
So let's give it a try and your terminal is run and it is our development server is running as expected.
So let's stop it for now.
Because of course we now need to create a new application for a project.
So I'm just going to move inside the first API online store folder and I can now even recommend Python
manage that by start up and we can maybe call it just products we can now go ahead and install the new
application from within the settings not by file.
Of course I'm just going to add products to the list of the installed applications.
We're now finally ready to start creating demo days for an application.
So we're going to need to model as a product model in a manufactured model plus manufacturer models.
And that's model and manufacture and model is going to add three fields and name field models dot charge
field.
We have Max length.
We can pass maybe one hundred and twenty then a location field which is going to be the same basically
as the name field and then a boolean flag that we can just call active that's start boolean field and
for these boolean flag we can just set a default of through of course in a real war scenario such boolean
flags are used to tell for example our logic if manufacturers should be listed or not least of manufacturer.
For example we can now define than the string self that can just return self name.
Let's now define our second model the product model.
Models not model.
And first of all let's link to the manufacturer to more than so manufacturer.
So and I'm going to use of course if Frankie so models that Frankie so linked to manufacturer model
then when to set on the lead models dot skate and then related name and we can set products.
Then let's add a name for our product.
So name and in fact we can just use the same code we use the above.
Then let's add the description description.
Model starts.
Text field.
We can't make it optional bypassing blank through and no through.
Then we can define photo fields so more does not image field.
Making it optional as we did for the description.
Price can set models dot flowed field and of course every product is going to need to price.
Then let's add shipping cost.
Shipping costs and we can use a float field just as well then quantity and we can define modules dot
positive small integer field and we can now define then their String def and then the string self and
we're just going to return self doth name.
Perfect.
So we are now ready to generate their migrations for our models and we can of course use the command
manage that by make migrations and we can now apply them.
Maggie we manage that by migrate.
Perfect.
Very well.
We're now ready to create our views within their views dot by file of our products application of course.
And as you probably remember one of the easiest way to create views in Django is to use the generic
class based views that are already provided by the framework.
So let's import both.
View and least view from Django that views dot generic dot detail import detail view and same thing.
I'm going to do as I said for list view so I need to change detail into list.
And of course we also need to import our models from models import product and manufacture it we're
also going to need to create some templates of course to use we have our views.
So I'm going to create a folder within the products app and I'm just going to call it templates.
We've seen this for they're now going to create another folder called of course products and we've seen
this folder.
I'm now going to create two files to HDMI templates so product detail dot HD e-mail and product list
that HDMI out.
I can just add some code here.
So this is going to be of course product at least I can do the same thing for product detail so HCM
out five product detail.
And let's now go back to our viewers notify fire because of course we actually need to write our views
to class product detail view and of course is going to extend digital view.
So we need a defined model as product of course and template name as products.
Product detail not HD email and we can now define also our product list view which is going to inherit
code from list view.
And so model it's going to be product and we can do something similar for it's a template name but it's
going to be of course product list both HDMI out.
So let's just this keep the manufacturing model for now.
Let's go ahead and write to code for our HDMI templates instead so we can actually start from the title
object that name.
And then I can actually copy object of name because I now want to create a couple of paragraphs to show
all the different details of a specific instance of product.
So product object a name
manufacturer
affectionate we're going to have quantity
we are going to f the description
price
object price and then of course shipping cost so shipping costs.
Then maybe we can add something like dollars or euros or in other currency.
Then of course we're going to need to show that image.
So the specific image for a specific product.
So object that photo don't you Earl.
Because of course photo is the name of the image field
product photo.
And for that product at least the template can of course make a for loop.
So for product in objects listed and we can just close it straight away so.
And for that we can just show maybe the most important details such as product manufacturer and maybe
price.
So I'm going to copy product and manufacture right here and price like so maybe give a bit of indentation
even though in fact this needs to be called object as object is the name that we're using to reference
all the properties.
So now that we've got our views are templates in our models we can define.
Are you a real parts.
So let's create a new file or else dot by.
And within this file first of all we need to import the path function.
So from Django dot you add else import path.
Dan of course we need to import our views so from viewers import product detail view and product list
view.
So let's now define your URL pattern as at least.
And of course path and maybe you can say he does the home page or they mean Beth product list view dot
s view if name product list.
And we can do something similar for product detail view some product detail but because we wanted details
of a specific product here are going to need to pass a primary key to our detailed view.
So I'm now going to change the path to product selection integer primary key.
And we now need to include the D you address that by file.
We've seen that it has stood by file of the main project so the main you arrested by file so let's import
the include function and we can now define the path to the let's say home page and we can now include
products that you add else because you're also using media files considering that we got an image field
in that product model.
We also need to tell Django that we want our development server to serve media files during the development
phase.
So let's import static and settings from Django dot com dot you URLs dot static import static and from
Django that's called import settings and I can in fact move all of these right here.
So if settings dot debug we can then specify that we want to add to at least of part static settings
dot media you URL document route settings not media route and of course considering that we are referencing
media you rail and media route.
We need to define them in that settings not by five so settings that by right here below so media route
we can just call it uploads and media your URL.
You can just set slash media for Slash.
So our project is basically completed.
There is one last file that we need to set up and it's the admin that by 5 which is going to allow us
to create new instances of product and manufacture from the admin panel.
So from models import product and manufacturer and then add mean that site The Register product saving
we can do for manufacturers so we can now close most of these files because we are not going to need
them anymore.
Maybe you can just leave our templates and we can run our development server so let's run it.
Entities system checking it into fight.
No issues with live at.
Look I lost on party 2000.
So let's open up our browser and ID we see that we are currently in our home page.
There is no content but how do windows title speaks clearly.
Let's go to slash admin instead.
Let's enter our super user credentials.
And of course we got both manufacturers and products.
So let's start by creating a new manufacturer instance I can just call it first manufacturer.
Maybe location Italy we can save the instance.
So let's add some products so manufacturer first manufacturer and let's add some by sequels to our product
list.
So Citi Bike and in the description field we can add a very comfortable
bicycle for the price we can maybe set eight hundred dollars shipping costs.
You can set it to zero.
Free shipping quantity maybe we can add something like twenty five so let's choose a photo.
So I'm going to choose this photograph and I can now just click save and add another from the same manufacturer.
And maybe this time we can add the racing bikes or racing bike very fast.
Bicycle
so let's choose a photo.
These specific racing bike.
And the price tag is going to be our year so maybe two thousand five hundred dollars shipping costs.
We can give it for free.
So 3 shipping and because this is a much more expensive bike we can maybe just add 5 in the quantity
field so let's save and let's go back to our home page.
So view site and here we have Tim the list of our products.
So let's check the details of a single product products this Lesh one and if we have the details of
our city bike now checking the details of the second product our racing bike and it is.
So that was it for this video.
Now that our line sort of project is ready and we've refreshed that a memorial now to create and manage
a Jenga project.
We're finally ready to extend its functionalities by creating an API in the next video.
This will allow us to expose a list with all the products in our database and the details of single
products using the chase on file format.
And of course this will also allow us to make a proper comparison with what we've done so far which
is using django as templates to show all the relevant information about their products in our database
and it will also allow us to understand how powerful a useful the Django framework package release.
So that was it for the media.
See you in the next lesson.