Build And Deploy a REST API with Django REST Framework #2 Project Set Up

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and welcome to this video in this video we're going to set up our application so we're going to be setting up our jungle application and setting up our database as well as the different things that we may need so right now i have an empty folder open within our visual studio code so the first thing is yours is to create a virtual environment so i'm going to open up my terminal i'm using bash and feel free to use cmd uh opera shelf for me i'm using bash because i'm on linux so what i'm going to do is to run python 3 because i'm on linux then minus m then i'll say them and this is going to create our virtual environment as a and v because uh we need to call virtual name as a command so then i'm specifying that my environment my virtual environment is going to be located within the fall that env now right after running this we have a folder called env and this is what we are going to have as our virtual environment so i'm also going to set this up as what you're going to use as our virtual environment so right after doing that then the first next thing is going to be to activate this so i'll say source env and when you're in windows you'll have something like scripts i'm online so i'm going to have something like bing and then i'll run the activate script and it's going to activate my virtual environment now right here you can see that our batch environment has been activated now the first thing i'm going to do is to install jungle so i'll go ahead and save pip install jungle so this is going to go ahead and install jungle within our virtual environment now after installing jungle i'm going to go ahead and set up our general project so think of a jungle predictor that is going to contain all the configurations for our project and onto this project we're going to be able to uh plug in multiple applications containing different functionalities so the first thing i'm going to do is to run the command jungle admin so every time you install jungle you get access to the jungle admin command that you're going to run to start a project so instead jungle admin then run the command stat project and then specify the name of the project that i want to create so in this case it's going to be pizza or pizza whatever we want to call it then i also specify that i want this file this project to be created within our current directory so the reason that's why i'm saying dot is because i want to get access to the monitor py file but i want to do that within our folder so when i press enter you currently see that our money the pi file is actually located in our root project folder so you need to go ahead and check if our python installation has worked well right python three and then do py and then run server now this is going to run our server so right now server is running at localhost 8000 so i'll press ctrl and click to access our server and when you see this screen our jungle installation has worked successfully now when we create a jungle project we have access to our manage.py file which is going to be the administrative file for the entire project so right now if i stop this server and run money.poi and then uh got the different options so we can see the different options such as flash inspect db and so on those commands are going to help us to administer application now within our pizza project we have the different files so the different files are settings urls wsgi and so on so the most important file right in here is going to be our settings file as it's going to help us to set up our application so h we're going to write the different configurations that we want for our python jungle app within our settings dot py so i'm going to close our time you know now i'm going to remove these comments now the first thing we're going to do is to set up some of these configurations as environment variables we may want to do this so that we keep our application secure and we don't expose these different secrets to external parties so chances are that you may put a record in a public repository and you may want to keep something separate so i'm going to do is to install python decouple i thought that's going to help us to make this environment very both sacred so you need to pull up my times you know and what i'll do is to set pip install python so in this case i'm going to say python decouple and this is going to go ahead and install python decouple within our virtual environment now after installing python decouple the next thing we're going to do is to set up our environment variables so you need to create a file right in here and i'm going to call that file dot env so this dot env file is going to be the one to keep our budget i mean our environment variables uh in our development setting so right in here we're going to be able to paste this and copy and paste some of these environment variables from our settings dot poi into our dot env file so i'll go to settings and the first thing i'm going to do is remove a secret key from my settings so you need to put a secret key right in here so you need to replace this default secret key with what you're going to write within our uh within our environment variables so i'm going to use python separates to generate a random hexadecimal value that you're going to use as a secret so within our python time you know i mean i'm within that have an arranged type in python and this is going to give us access to the python interactive shell so i'm going to import secrets and right after importing circuits i'm going to go ahead and generate a random hexadecimal value so i'll say circuits dot token and in this case and generate x so i'll specify the length so i'll say 12 and this is going to generate this hexadecimal value so i'll copy it and right after copying it actually i'm going to copy it with ctrl shift c and right after copying it then i'm going to go and replace this with in our dot env file so i'll say the secret key is equal to the random hexadecimal value right after setting it then i'm going to go and access this within our settings file so to access this i'm going to be making use of config which comes from the couple so i'm going to go to the drop of our file and then save from the couple i'm going to import config and right after importing config then i'm going to go ahead and specify that we want to access our secret key so i'll say the r secret key is equal to config then i'm going to basically pass in the key which is the name of asset credit key so this is going to be circuit key so i'm going to also look for other environment variables so we may want to set debug to true in development and then set it false in production so i'm also going to copy a debug value so i'm going to remove this and then say in this case we're going to have our debug value as debug so i'm going to also set this within r dot e and b so i'll set the pack is going to be equal to true this is for our development environment so right here i may say config is equal to debug but by default uh our python decouple gets it as a string from our dot env so we have to specify which type to cast it to in case we want it as a boolean so i'll come right in here and say cast is going to be equal to a boolean so when i save this i'm going to go and basically look at other settings or writing here we don't have any other settings these database settings will change we enrich the time for deploying so hoping that everything is running fine i'm just going to pull up my terminal stop the interactive shell control z and run our server again so right now we enter in our server python monitor poi and then run server and this is telling us that we have 18 and applied migrations so i'm going to stop this and when i stop this i'm going to run our migration so i'm going to run python3 money to py and then this is going to be migrate and this is going to go ahead and apply our different migrations on our database so right after doing this then we're going to go ahead and set up the different applications we want for our for our project so the first application i'm going to set up is one for our authentication so the authentication application is going to have the different logic for creating users as well as sending in users as well as rdwt authentication specific routes so right right in here what i'm going to do is to say python three monitor py and then i'll go ahead and run the command start app and then the name of the app we want to create so in this case we want to create that auth app so i'm going to call this authentication so i'll call the app authentication and this is going to go ahead and create our authentication app so if i check in our folders right here we have our app created so the app has different files such as the models where i describe our database the views which contain the different views that you're going to create that are going to return http responses as well as the admin that's going to help us to adjust our models that we so that you can be able to access them on our admin site so we have the apps which contains the different uh config that's going to help us to access our app at the project level so actually what i'm going to do is to copy this app name and right after copying this the name of this configuration i'm going to go ahead and register this up within our settings so i'm going to go to settings.pay and i'll look into our installed apps list and then be able to add our app so i'm going to come right in here and basically paste this so i'm going to access this class by referring to the app name then the app section the apps model in which it's located so i'll come and say it's going to be authentication so within authentication they're going to go to apps so i'll say dot apps and then dot authentication so this is going to actually be dot authentication config so i mean save i'm also going to go ahead and create our our orders application so i'm going to come and open the time you know i'll do the same thing for our orders app so i'll come and say or does so when i run this we now see that our orders app has also been created so i'll do the same thing i'll go to our absolute pie within our orders and then copy the orders config now to register this i'm going to head over to our settings the poi file and i'll do the same thing so i'll come and set so in this case the app name is orders then i'll specify that the module is apps actually it's going to be apps and then we shall paste in the name of our config right now i'm going to save so right after saving the next thing we're going to do is to be able to create our first api views so i mean to run our file so i'll say python 3 money.poi and run server so right now we can see that we our server is running uh successfully so we don't have any errors so what i'm going to do is to go ahead and install january's framework now as i said in the introduction genres framework is a set of tools that's going to allow us to use jungle to build a api so i'm going to go ahead and install it so right now if i run keep this we're going to see the different things we've installed for our app but you don't have generous framework so to install your race framework i'm going to set pip 3 install then i'm installing jungle rest framework so this is going to go ahead and install january's framework and after installing generates framework i'm going to basically create our requirements.txt that's going to contain the different dependencies for our applications i'll say pip 3 and then freeze so i'm going to direct it to that requirements the txt file so this requirement is the txt file is going to be created as well as edited so for example if i open the requirements.txt file i'm going to see the different things we've installed which includes django january's framework and all the other project dependencies so now let's go ahead and make use of generation mark so the first thing i'm going to do is to add generous framework to the list of our installed apps so i'm going to go to our install apps list and then what i'll do is to add iris framework actually i'm going to separate these up so i'll say these are going to be a third party apps so the first i'm going to add is going to be our rest framework now after doing this we get access to all the functionality that genres framework comes with so to do this i'm going to go ahead and create a simple api views that you may use to basically test if our apps are working so i'm going to start with our authentication app we need to go to our authentication views and then within our authentication views we are going to create our first api view so what i'll do is to come and so i need to make use of jungles generic views most of the time so i'll come and say from rest framework actually i'm going to make use of first framework is generic field so this is going to be rest framework i'm going to import the generic views so these are going to be the generics and right after doing this thing we're going to create our first generic view so i'm going to say create a class and this class is going to be a generic api view so i mean say it's going to be a hello i may call it a hello of view so just going to call it a hello earth view and what this is going to be it's going to be a generic api view so i'm going to say generics dot generic so it's going to be a generic api view and right after doing this i'm going to specify that method so in this case we're going to just get rid specify the get method and then it's going to take in a request then we're going to return a response so rest framework allows us to return responses and determine which kind of data and the status that we expect as a as a response so we're going to import response from rest frameworks ourselves from race framework dot response i'm going to go ahead and import our so this is actually going to be our response class so it's going to be response so our comment returned our response and then the first thing we're going to specify is the data that you want to return for this response so you said the data is going to be a simple dictionary containing a message and then also going to specify that our message is going to be hello auth so i'm also going to go ahead and specify the status we want to return this with now the beauty of investment mark is that it allows us to be able to get statuses so i can say our status is going to be co2 so i'm going to also import status from rest framework and then go ahead and basically give information about the status that we want to return in this response so i'm going to go ahead and specify so basically it's going to be a message actually our status is going to be an adjunct to the response going to be an attribute to the response so i'll say that our status is going to be equal to so i'll specify that this is going to be status but http then the status is going to be 200 and then okay so this means that we are returning a message of hello auth and with the status code of http 200 and this is going to be okay so for our api view to work we're going to need to register this to actually map it to a url so that you'll be able to access it on an api so i'll go within our authentication application and then create a new file so you need to call this file urls dot pi and within urls.pi i'm going to trade the different url patterns that are going to contain to be of that authentication app so i'll come and say from jungle dot urls i'm going to import that so i'm going to import path now path is going to allow us to basically map each view function or each api view to a url that you're going to access on that api so the first thing i'm going to do is to create a list so this list is going to be a list of url patterns so it's safe url patterns is going to be a list and now this list is going to contain the different url patterns so for us to create a url we're going to specify our path and then we're going to also dictate the regular expression of that specific url so in this case it's going to be our url so we might actually call this uh both and then actually let's say this is going to be at our root of our authentication urls and then we're going to go ahead and specify our view so i'm going to import this i'll say from dot which is our current folder i'm going to go ahead and import i have this or save from dot import views and then right here i'll say is fuse then we're going to import the hello off view this is a class-based view we shall need to get it as function based view so we have a method called that dot as view method then we're going to also specify a name so in this case let me provide the name as let's say hello both now after doing this then what i'm going to do is to go ahead and basically register these urls onto our main app so if i go to our main app which is our pizza uh this is actually our project so when i go to the projects url so right here we can be just a different urls of every application that we have so i'm going to remove this comment and the first thing i'm going to do is import include now the include function helps us to be able to access urls in external apps so what i'm going to do is to import include then go ahead and basically add this onto our url pattern so i'll say path and then specify that these are going to be the author out so they'll be accessed on the prefix of slash off so then i'll go ahead and include our urls from our authentication app so in this case i'll say include and then specify the module in this case it's going to be authentication then you're going to specify the module containing our url so i'll say dot urls now this is going to be uh looking for the urls.pi file within uh our authentication app and as long as this urls.pai file contains the url patterns then those urls are going to be those urls are going to be accessed on our api so when i save this hoping that our server is running i'm actually just going to run it so when i run a server nothing is wrong so when i head over to our browser and refresh right now we don't have any uh url so when i head over to that slash auth in this case we see that our api is returning a message of hello oath so the beauty with january's framework is that it has this browseable api that can use out of the box so right now we have uh our message as hello so let's do the same thing for our order so head over to our orders with pay so i'm actually going to copy the same functionality so i'll do is to copy the code rating here and i'll use the same code i'll change a few things so i'll go to this.pack and then right in here within obvious i'm going to basically change a little bit so the first thing i'm going to change is going to be the name of our class so it's going to be hello order view and then right in here i'm going to also change uh hello for this and this is going to return the same status quote so when i save i'm also going to go ahead and create i urls.5 so it's going to be urls go to pi and then i'm also going to copy that same urls so i'll go to our authentication app i'll do the same thing so when i go to uh urls for orders i'll paste the same thing now instead of hello auth what i'm going to do is to call hello so this will be hello order view and this will be the same so when i go to uh main project which is pizza now go to urls.pai i'm also going to go ahead and include the same url so in this case we're going to access all other endpoints on the slash or that's prefix so we shall access all of them here so i will have this going to be include then i'll include orders dot urls i'm going to go ahead and save this so when i go to our client here and i refresh when we open our server so i'm going to go to localhost 8000 slash or das and in this case we are going to get hello orders in this video being able to set up our program so we have been able to set up our project we have been able to set up our different applications and add different environment variables thank you for watching and if you like this video please go and leave a like please subscribe to the channel if you haven't done so thank you for watching and see you in the next video
Info
Channel: Ssali Jonathan
Views: 28
Rating: 5 out of 5
Keywords: django rest framework, django rest framework tutorial, django, django rest api, django rest, django framework, rest framework, serializer in django rest framework, django tutorial, rest api, rest framework django, django rest framework api, уроки django rest framework, django rest api tutorial, python django rest framework, django rest framework viewsets, django react, django rest framework full tutorial, django rest framework tutorial for beginners
Id: 986a19NBTuE
Channel Id: undefined
Length: 23min 35sec (1415 seconds)
Published: Tue Oct 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.