Django Rest Framework Full Crash Course | Build REST APIs With Django

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial we're going to be talking about the django rest framework so the django res framework is a library which allows you to build apis in your django project so let's say you have a django project here and you want to build an api so that others can access your data or you are just trying it for fun or just want to build your skills the jungle rest framework is the best way to go it allows you to easily build apis with a lot less code so this is this the rest framework is very very easy to set up and i'm going to walk you through everything in this tutorial so we're going to start from the scratch we're going to first of all install jungle and then we're gonna start a new django project then we're gonna install the rest framework and then i'll show you how to integrate the rest framework into that django project so without wasting any time let's get straight into it so right here i have my command prompt and i already have it in this directory where i want to create my jungle project i want to first make sure that i have django installed so i'll just pip install jungle now this command line is for installing django using python package manager called pip now you can also install jungle in some other ways like easy install or just with the github code directly but this is the best way to go so i already have django installed so it should be telling me requirement already satisfied so we should give that a minute it should bring that up now we already have django installed right here so it's taking longer than usual and then i can just cut out of that yeah so let me just close but i have django installed but for you you should go ahead and install it so now let's start the new jungle project using the django admin start project and then let's say drf which stands for django rest framework let's say approach so junglers framework project so that command line is going to start a new application or a new jungle project in this directory so this is a directory right here so you can see now that it is empty for now and boom you see that we have a new project that has been started so let's come back yeah so now that we know we have a new project started let's just cd into that project the ir first of all so we see we have django project and cd into it so let's close that up if i press the ir now i can see that i have managers py and dif project so this is my standard django project now the next thing i want to do is to install the django rest framework before i install junglers framework let me bring this jungle project into vs code so let's open up vs code we can close this up and then in file i just gonna come to open folder and then it should take me to the folder which actually take me to here the file manager so i can go into that folder i think it is drf toots then junk dif approach so this is the folder and then once i select that folder obviously vs code is going to open that up so as vs code is doing its magic let's come back to the command prompt and then what we just want to do now is to install the django res framework so let's say peep install django rest framework so this is how easy it is to install the jungle rest framework just a simple pip command so this command line is going to install the jungle rest framework on our computer so again i already have the jungle rest framework installed so if you tell me requirement already satisfied but for you you should go ahead and install that if you don't have that installed so let's come back into vs code as that is installing and then let's just make everything clean here so we can quit this so you can see that this is our jungle project so for this project we are not going to create a jungle app at least for now so later we are still going to create an app where we are going to use serializers and stuff like that but for now we're just going to stick with just the django project and the file in it so that's we're going to put our views.py and everything we're going to be using so let's come back to the command prompt as you can see it says requirements already satisfied this means that i already have the rest framework installed so now that i know i have the rest framework installed i can use the library in the module in our project so now what i just want to do is to create a new file first of all and then let me name it views dot pure so in this view the py file that's where i'm gonna code just like a normal jungle project first of all let me import render say from jungle.shortcut so from jungle.shortcut is the command line [Music] not command line actually is the module so the shortcut is like a class i think like a function in the jungle library in which you can import some things so we import render and now that is just for rendering a normal template file but i want to use the api view from the rest framework so the rest framework provides us with an api view class or function or whatever it is so with that api view we are going to be able to access a lot of type of apis that is available in the jungle rest framework so when we use the api view we can do we can use something like a get request or put requests and some other things that are being rendered or have been given to us by the rest framework so first of all we'll save from rest underscore framework and then we want to dot views then we import api view so i put in the api view from the rest frame of those views and then as i said this api is going to allow us to create a function or a class on it so that we can be able to use all the everything available in this api view but what also is an api view we want to obviously send a response now for example let's say someone another developer tries to access our api and once it sends a request to our api we want to give the developer a response we want to give him some sort of results so let's say user send a get request just to get a list of a query set or something or a list of data i want to give the user back a response of that data so for us to give a response we're going to have to import response from the rest framework so say from rest framework import dot response first of all import response so what this is doing is just saying from rest framework dot oh nope this is wrong from rest framework dot response you just want to import response so that's it now that we have this everything imported we can now create a class to inherit from this api view so that we can get a lot of method that we can work with so let's just have a new class and let's name it something like let's say yeah that you can see this is test view and then api view so this we're just naming this test view and then this test view is in everything from the api view which is right here so what we can do now in that under this class we want to have a get function which is just going to be like a get request to this api so say deaf get and then we have to say self request x that's argument and then keyword x that's keyword argument keyword x just like this so now that we have this function set up what we just want to specify is a data so this is a test view and then in this test we have a get request so we want to like have a data in which we're going to send back to the user or just give us a result so say data for now let's just add code our data ourself later i'm going to show you how to use the django model so that this data will be like from the database you have or something but for now let's just have a simple dictionary that we're going to send back so let's just have a dictionary saying something like username let us just say admin and then the number of years let's say like number of years that the user has been active and then we can give it something like 10 years so that's actually a good user so this is just the data the username is admin and it has been active for 10 years let's just make the most specific number of yes active so let's just remove this we don't need that so just yes active so that's 10 years so now we have this data that is in a dictionary format so this is the data i want to send back as a response so what i'm just going to do is to say return response of data so what we just did was that we use this response in which we imported from rest framework.response and then we're returning a response of this particular data so now we have this done what we want to do now is we want to go set a url for this so that we can actually test this when we run it on our localhost so now let's come into urls.py right here i just wanna remove all this don't need it and then in here first of all let me have a new path which is going to be empty now this empty part shows the home url so like when you just come to your website um let's say like google.com that home url is that empty path so that's that now what we can just do is to first of all import that test view from dot views let's import test view so now that we have that test view imported we can easily use it in here so this is the ohm and then we can just say test view since it's a class based view we have to add dot as view now the reason why we're adding this dot as view is because the view in which we are using here is a class based view if you as in a function based view you don't need to do that you can just say test view here as the view but since guys in the class base view we must do this even though it's going to give us an error and django is not going to recognize that as a valid view so test view dot as view and then let's just give it like a name of something like test [Music] so now we have all this setup we have it linking to the home url using the test view that has view and we gave it a name of test now i want us to just run our server and test it immediately but if we do that why ain't gonna see nothing we might even get an error and the reason why i'm again an error is because right here what we did was we installed the jungle rest framework and immediately we just came to start importing everything in our project but you know if you work with jungle for a while you know you actually need to do some second configurations to be able to use some particular type of libraries not all so for this django rest framework we need to add some things to our settings now let's come into settings.py file first of all the first thing we need to add is in our installed apps we need to add what we are the rest framework unsurprisingly so right here we need to address framework in our installed app so django is going to recognize it now after adding that that's all to do with the settings what we just want to do is under the urls you need to add one url in which the rest remote recognize so first of all we need to import include after that we can just do path api iphone earth and then we're just gonna do like a comma let's give this a slash and then we're just gonna include from the rest framework dot urls so that is that for that i'm gonna give it a comma and then we can save it so now we can easily come to our command prompt and just do python manage this py run server so now when we run this server we can come right here and then if we go to the home actually we're going to use this for now we just have to set that up because the jungle red remark requires that so when we come to the home it should give us this test view and this test view should just be like a get so just like is sending a get request and then we're just gonna give it a response of this particular data so let's save this also and then let's come back and see what's going on so this should start running in a second let's wait for that normally django doesn't take time to run but i guess because it's a new project and we're running for the first time so let's just give it a minute okay so we have that done and then let's come back to our browser let's open a new tab in the new tab let's go into our localhost with the port of 8000 because that's jungle defaults url or something like that so let's give it a time to load okay i can see the title loading already let's give it a few seconds and yeah good so this is what we see now you know that we didn't design all this page or this fancy ui that we're seeing we didn't design it that's what comes with the django rest remote it actually gives you this nice looking template to showcase your api and basically test all your apis now as you can see is giving us in a dictionary right here it's giving us username admin yes active10 and that is exactly the same thing we have right here in data so normally the apis uses it gives us a response in json format now it's giving us a response in dictionary format because that was what we actually set up right here normally it should be a json format so whenever dealing with real data remodels you're going to see what i'm talking about so as you can see it's using the the get request and then the http 200 says okay so because it's allowing a get request because we specified the get function right here um let me remove this save because you specify the get function right here if we specify the post function we're also going to talk about that soon it's going to show right here that we are allowing a post request so that is basically the basics about this jungle rest framework so if i come here and then i it's json you're gonna see now that it gives me this in json format so right here it just gives me in a blank json format no template nothing just a blank json format so that's if i go to this url but normally we just it's the home page this is what it shows us so this is good and i hope you're starting to have a grasp of what the jungle race framework is about now to be honest it gets more complex than this later but i'm gonna walk you through every single thing you need to know to get started and i'm gonna simplify every single thing so let's just come back to the rest framework page and if we scroll down right here in installation you see that exactly what is done here is what we have done in our project so you can see it says install using peep including any optional packages you want so this is where we install the django restrainment we can also install markdown on the jungle filter but we don't need that for now because they are optional packages so you just install the jungle rest framework what is mandatory or you could just clone the project from github i see that as stressful you know so i just prefer using pip so what we just did was to address framework to our installed apps right when we did it in the settings and then we added this url pattern that i said the rest framework requires so we've done all that we've done gone through the installation we've come here and we've seen the results of the apis and then let's move straight into jungle rest framework serializers now let's talk about serializers in the django rest framework so serializers is a structure or a representation that represents a data we want to return in a json format or accept in a json format so we can use a serializer to like transform our django modules into json let's first create a new app on our jungle project and then we're going to look deeper into serializers so right here in where where is that in my command prompt i'm just gonna opt out of this server that is already running and then i'm gonna create a new python a new jungle app for this smart project and i'm going to name it whatever i want to name it maybe my app or drf app or something so first of all let's opt out of these so this should be gone within a second and then yeah yeah yeah yeah okay so okay well i was a lot oppressed so now we can just do python manage.py start up let's say drf app so this is going to start a new jungle app in our project so when this is started we're just gonna go at it and continue with it so um first of all after this is started we're gonna go into our models and create a model that we're actually gonna play around with so let's just wait for this to do and that should be done within a second let's give it a moment to do and see so this doesn't take time and um so as that is doing let me talk more about serializers actually i want to explain more about serializers because when i get started with the rest framework that jelly takes a bit of a while to understand what these things actually do so the serializer is think about it like a form like a model form once you have a model the way you create a form for model so you can submit in jungle so you can submit your form or update or whatever you want to do think of that as a serializer so serializer is just basically the same thing you have a model then you have a serializer that you link that model to and then you specify the fields you want to be you want to be in that particular serializer and then just use it in your views so that was the theoretical part now let's dive into practicals so right here we can see that drf app has been created successfully good just remove that so let's come back in now and we have to create a new file and name it serializers dot pure so now this serializes that py is the file we're gonna configure our serialization for now we're gonna leave this blank now as i said we're going to create a new model that we're going to use so let's come into module.py so right here in modules.pir let's just create a new model now i want to name this model like country oh i changed my mind our name is student and then you're gonna be from the models that's model so let's give it some fields and now let's say the name of the student that would be models dot chart field and the character field should have a max lens [Music] and let's just say of hundred and let's give it another one like age [Music] as models that's integer field and i don't think that takes any attribute and let's give like a description [Music] of the student models dots it should be a text field and that doesn't have any attributes and let's say dates and roots and that should be a date time field so there'll be models dot date time field and let's say auto now should just be equals to true so i think we have these um yeah i'm happy with these and so let's just give give it a string yep that and then let's give it a self and then let's just have it return self the name so now we have this particular module now you know normally in jungle when you create a new model or you make any changes to the models file we need to migrate it into our database so we're going to do that right now but before we do that i want us to first of all register this app in our installed apps so coming to settings and then right here what we're just gonna do is to say drf app that's the name of the app we created so let's save that so now that we have that saved what we can just do is to come into our command prompt and then let's just migrate so say python manage the py make migrations so we give that a second to make the migrations it might say no change is detected so i just like making sure i make migrations and migrate so i don't get any errors because sometimes jungle might be funny so that should be doing okay yeah so as you can see it says creates model student now let's just go ahead and migrate so as you can see right here when we said money does people i make migrations what it did was just to make some migration of that model student and right here is just migrating every single thing into a database earth admin of drf that is our app and that is what we want to see so now that we have that migrated let's go back into the serializers.piva so now we need to import the serializers from the rest framework and we also need to import the post model from the module.pyfa and after that we can create like a serializer for our post model by specifying only some fields so first of all let's do from rest framework import [Music] serializers so now this is how we import the serializer so we can use it just like the way you use forms in jungle i think from django dot forms import forms or something like that that's just the way we import the serializers so let's also import that model in which you want to use so from dot models import students now we have these two things imported we're just gonna first of all create a serializer class for that student so say class will be student [Music] serializer and then it's going to take from the serializers those model serializers so as you can see it's quite similar to the form where we just have like if it was a form want to use we can say student form and it will say forms.modal form very similar if you are used to using the jungle forms you definitely definitely understand this so now let's just have a class meta and let's specify um [Music] let's specify the model mode there will be student and then the fields so the fields let's make it a tuple and then let's see let's just take two so we just want the name and then the edge so these are just the two fields we want to use in this serializer now it is that easy to specify a serializer to configure for any what you want to use so now let's say you are back to the views dot py file so right here so right here we're gonna import our post serializer and our post model so to do that let's just say from that serializers imports students so i actually made a mistake i said from the serializers i said we are going to report supposed serializer but we are only supporting pulse realizes why positive students realize that we actually don't have post but i'm just kind of used to saying post as like a model because that's what i use for examples most of the time so while i'm putting the student serializer right here class student serializer so from dot serializers which is these let's make sure we have that so from just serializers we're importing the student serializer let's come here good so now that we have the student serializer imported let's just import the student model so from the models [Music] import student [Music] so now we also have that imported let's come here and make sure that's correct student yeah that student so now that we have those two things imported let's create a post method in our test view class so in this test view class we're going to create a post method so we can receive data just like in a form so to do this we'll just add a function below in our test view class so right here we'll say deaf post now this is going to be a post not a get and then it's just going to take exactly the same thing it should be self exactly the same thing and it's going to be self request ads and then keyword x so now that we have that we're just going to specify our serializer in a variable so say siri alive equals to student serializer and then let's just give it a data so when we do something like data equals to request so we'll do something like request dot data so when we do something like this what it means is that we are actually we want to use a form or want to submit a data or something like this once say data equals request.data so beneath we're going to say if serializer that is valid so first of all check if it's valid what we just want to do now is to say serializer dot save just exactly the way we do in a django form i'm always referencing to that jungle form because it is very similar to this as i said earlier if you've worked with the jungle form before you understand what's going on right here so serializer that's data so what i just did was that first of all specify the serializer now that serializer is this student serializer so normally one is in a post like if we don't want to submit a form we can just remove this answer serialization student serializer but i said because we want to submit something like a form we have to say data equals to request the data it needs to get the data which is being posted into this particular api view so when we get that we say if the serializer dot is valid that means if all the values are valuable or are correct just like we were if we want to collect an integer field of course we want to have an integer field if we are supposed to call it an integer field and we have a character field or text field then that serializer is not valid so this is just checking if it's valid if all everything we want if all the values are correct and if it's valid we just save it so serializer.save and then we just return the response of that particular data which the user saved so you know just like when you save a data we just want to show back the user that sources of data has been saved this particular data has been saved but if this don't happen we'll just return a response of an error so say serializer that's errors so that is it so now let's save this and then let's come here and just run our server once again so what i want to do now that server is running we want to test this particular api in postman so let me first of all wait for that to load up so okay that should be done okay so it says module not found no module named drf project serializer what this is saying is that if we come back to our code it didn't find a particular okay good so what this is saying is that it didn't find modules.serializer that multi-industrializer in drf approach so when you put in this.module.serializer we're supposed to import it from here but as we can see we're importing it from here so normally we're supposed to use like the views.py in our project actually we can still use it in our in this place normally suppose using the app but we can see is in the project we just need to do is to make sure that we are bringing it in from here so let me just say from dots trf app dot serializers and then from the drf dot modules so we can just save this and that should work perfectly fine so now let's see let's wait for that to reload and then it should run without any errors so good so as we can see everything runs successfully now as i said let's come back to our browser as i said now we need to test this api so there is an application called postman so let me open a new tab and show you to get postmod you need to do is to type download postman and then once you type that you just hit enter and search it so postman is an application or software anything you like to call it which is used for testing your api so since our app is not in production yet our project is not in production it's on live on the web or something for us to test it locally on our localhost we can use this application called customer so right here is going to take you to the website where you're going to download it good so it's going to automatically detect your os so as you can see it's changed to windows because i'm on a windows actually download it just install it like a normal app or the way you install normal software on your os and you should be good to go so now that we have postman installed i have it opened right here what i just want to do is to create a new tab right here and then we're going to test that api in which we just created first of all i'm going to come in here and then copy so i'm copying that url because that's the url i'm gonna be testing anyway so right here i'm just gonna paste this is the url i'm gonna be testing and i wanted it to be a post method so right here i'm gonna come into body so and then i'm gonna click on form data now i'm gonna input key so this key is what i want to submit so let's see if we come back into our vs code and we come into our serializer we can see that the fields required and name and h so and we know that in our model's name is that character field and age is an integer field so to avoid any error we need to make sure that we abide by everything so name let's give it a value of admin and then key that is the age let's just give it a value of 25. so now if we send it's sending a request this is our postman works it's going to test that api for you and then good so as you can see right here name it it gives us a response of this particular data name admin age 25 but how do we know that this particular data has been submitted into our models so what we want to know is as this data have been really saved in our database for us to be able to do this we need to check we need to open up our admin interface and set that up our admin panel and then check our database so let's quickly do that first of all let's come back in here we're going to cut out of this server so now that we're out of the server what i want to do is to create a super user so i'll say python manage dot py create super user so this is gonna prompt me to input my username and my email or my password just like i'm registering for a platform of a website just exactly the same thing so let's give it a moment to yeah so i'm just gonna say admin i'm gonna leave this blank i'm gonna give it the password yeah bypass and good so now let's run this server again as that is running we're gonna come into our vs code and in the admin.py we're just gonna first of all import from that models import student and then we want to register that student in our admin interface so admin decider register students so now this will show that it has been registered in our admin panel we're going to check that in a minute let's make sure this is running good let's come into our browser let's open a new tab and let's go to slash admin so let's quickly do that right now and then let's login using the admin info okay good so now we can see that on that drf app we have students if i click on it we should have one object in there so as you can see we have one student which is admin and we submitted that from our api we didn't create this from this admin panel or from our project or from our website it's through our api now let's go test that one more time so we have this api we have the name and let's just give it something like admin 2 and then admin 2 is 27 years old so now when i send you can see it gives me admin 227 years old now the reason why it gives me this is because if i come back here in our code we'll see that i said that if it's valid it should save that data and it wants to save that data it should give us a response of serializer.data so that means if that is successful the response i want to get is the data in which was submitted so if i come back to postman anytime i get the data that i inputted here i submitted it means that i was successful so let's come into our admin panel each refresh now we have another one right here so that's how to submit from our api now that is cool now let's talk about serializing the data that is going out and i mean the get method so first of all we we already have our model registered we already have created a super user now to use a get method we're going to come back into our vs code now what i'm saying is as you can see this get method right here when we did it earlier is just a blank data just a normal dictionary but in this post method in this post function we are using serialization we are serializing our data so let's say i want to also serialize the data in this get now what we're going to do since we already have self request agreement and keyword argument we're going to specify a query set so let's get rid of these and then let's say like a query set that query set can be student dot objects that's all and now what we can just do we can say serializer can be equals to student serializer and they want to specify that query set and we need to specify one more thing saying many equals true now the reason we need to specify many equals true is because this query set is a list of objects so from the student model we're getting all the objects and when what i mean by object is right here all the data we have in this student database the student database or the object so as you can see it is more than one it's going to bring a list that's where we have to specify many equals true that means is more than one so after specifying the query size in a variable i just have another variable named serializer which is taken from the student serializer and i pass in the query set and then i have to tell you that many is equals to true now if it was just one data one but yeah just one particular object or something we can just put square reset but since it's more than one we have to put many equals to true so now that we have that we can just return the response of serializer the data so when a user try to access this api using a get method you're just going to return a response of this serializer.data and this serializer is a list of the objects in the student model which is basically all the data in that site so now let's save it and then let's go test our api once again but this time around we are just testing with a get so get we don't need out those let me remove this first of all so it doesn't see it none yep so now let's just hit send and as you can see it's just giving me a query set a list of the data we have in our project so it gave him in a json format now if you don't see this this particular square bracket then that is just a dictionary but since we see this bracket and curly braces which is now taking each of the data now we know that it's returning it in a json format which is the normal and standard way of returning api response or request so that is what we we can do when we're talking about gets serializing our get request but as i said in here i talked about many equals true but let's say it's just only one data how do we do that so first of all since we have this query set which is all the objects and we know that all the objects more than one so first of all let's get one that's only one of the objects so we can say student [Music] let's say student one should be equals to query set dot first now what this query set of first does is that it's going to get the first object in this student model so writing is realized that we just student serializer and then we remove many dots true and they were not passing the old query set now we're just passing only the first student which is student one so now we can just return the response of serializer.data so let's go and test it once again right in here it's gonna get so once we send as you can see it just gives us only that first one so that's how we can easily serialize our data in the jungle rest framework now let's talk about authentication in the django rest remote so what do i mean by this authentication now this authentication is going to allow us to protect our api endpoint so we might have some api that we can just allow anybody to use without authenticating or without authorizing that user that's fine but we might also have some apis which we want a user to first of all be authenticated i want the user to have an api key or a token or something before you can access that particular api just for example when you use the youtube data api you need to provide an api key which you get from your google cloud account or something like that so that api key is going to be used to authorize you or to authenticate you to be able to use their api so let's do something similar in this tutorial first of all what we want to do is to come into our code right here in views we're going to use something called permissions so these permissions is from the rest framework so if i say from rest underscore framework dot permissions import so when i say that rest remove the permissions import we are going to get a bunch of permissions a bunch of authentication lists so like i can use allow any now this is quite self-explanatory right click on it allow any it just allows any access this isn't strictly required but you could use an empty permission class list just to be so what this is just saying is that you can allow anybody to use this particular api so let's also see another one like is authenticated so you can see we have this admin user is not dedicated certificated or read only so we have a bunch of it so his admin user that is also safe and explanatory it has to be an admin user before the person can access the api or is authenticated this is the one we are going to talk about so the user has to be authenticated into this application before we can give the user the chance or if we can give the user the way or something to be able to use our api so now let's import is authenticated this one so now that we have that imported we can use it in our test view class to do this we can just add the code right here so let's say before any of the functions let's just add a variable name permission classes now this permission class is going to give us like the permission we want to collect before a user can access any of these so mozilla's have been dedicated can anybody just assess it must be an admin user or whatever so it's gonna be a tuple and then we'll say it's authenticated we want the user to be authenticated let's give it like that so we only want authenticated now if you want to allow any that means anybody you can also just leave it blank like this this just means that anybody can access it or you can just remove that anyhow you want to do it so but it's not dedicated means that a user needs to be on dedicated before you can use it now let's save it and try something if we come into postman and then we just send it's going to draw us an error so as you can see okay this is not the arrow we are looking for so this is just saying that our host is not running so let's come in here so okay it's running right now now let's try that again so it's gonna give us an authentication error good so it says detail authentication credentials we are not provided so now as you can see it requires us to be authenticated or to provide some authentication token or something before it can give us access to our urls to our apis so now to be able to to set up this authentication we need to come back into our code and in settings.py going to scroll all the way to the bottom and then right here we're going to say rest underscore frame work should be equals to the curly braces and then we'll say defaults underscore authentication underscore classes and then now what we need is a square bracket and then we say rest now this should be in small caps rest underscore framework dot authentication that's talking authentication so this is what we need and then now we can save this so we can click this file and then now that we have that now to use this rest framework we also need to actually add something let's go back to that settings to the installed apps so scroll up and then in the installed apps we want to add rest framework so well as you can see we already have rest framework added but what we want to add is restframework.org talking so that is different and then we just save it so rest framework the token now we can quit it but before we continue we need to run migrations so let's opt out of this server python manage wpy make migrations so after making migrations no change is detected actually so let's just monitor p1 migrate so it is going to migrate to that res framework.outstalking into our database as you can see all talking are talking or talking now let's just run our server back um actually i don't want to run the server back so let's quit it out want to do one thing so first of all i want to flush all the data we have in this our database so we can like show up can show you how to create a token for each user so now we still have a user well let's flush every single thing so say money.py flush so this is going to flush all the data we have in our database just click yes and then done so now we don't have any user if i come in here i should actually be giving an arrow that i'm not dedicated or something so okay it can reach the page because it's not running but normally the admin user is deleted but now we want to create another super user a new super user say python money.py create super user so right now it should prompt me to input username so username leave that blank [Music] why good so now we have that created now let's run our server back so let's give it so if we come back here now into our server so it acts us to login again because we flushed all the data at first so we log in good so if i come to home you're gonna see now that i have uh uh talking app and then under it have a talking model so this is what we just did so a good way to create a token for a user one easy way is to just come into our command line let's opt out of the server again and say python manage.py drf underscore create underscore token and we just pass in the username so this is going to create a token for whatever username we're passing so let's wait for that it's going to give us a token right now and it's also going to save it in the odds token so as you can see it gives us this token let's just copy that and if we come in here and it's refresh on this model we're going to see that we're going to have one object okay so let's first of all run our server back before we go test it out just to make sure everything is working so now let's hit refresh on this token now good you can see now that we have that token right here the same thing start would b and with the c that will be and with the c for the admin user and created at this time so this is uh what we how you can easily create a token for a user so now this user has a token let me now show you how you can use this talking in postman so you can authenticate so normally we're just going to come into our postman to authenticate using that stocking what we just want to do now is to go under authorization and then let me first of all put this down a little bit so the type we want to change this type to api key so it's giving me this because i've tested some earlier before so let me just remove this and start from scratch so the key the default key for jungle should be authorization authorization and the value how you're gonna write is gonna be talking and then followed by that particular token which was created for the user this token which is the same thing as this talking so let's go back to postman so right here now and we need to make sure that we're adding this to the editor not the query parameters so let's bring this up a little bit and it's sent so now it doesn't tell us now again that the credentials that need some authorization credentials as you can see it gives us a json response it says name is blank age is null and this is because remember that we flushed our database earlier on so that's what's giving us now if we have something in our database it's obviously going to show that to us so that's how you can easily authorize or authenticate in your jungle stream but now you know that let's say you are using an external framework like you want to use react or something in this your project you don't want it to you can't do this manually like as you can as you saw we have to come here to type python dot py drf create token admin before we able to generate a token for this user we can't do that for everybody on our platform we want it to be automated i mean that's the main purpose of programming right so what we are just going to do is to send details of the particular user so it's username and password we're going to send it to our application and it's automatically going to generate the token and show us that token so first we're able to do this i'm going to come into vs code and under urls.py we're gonna say from rest framework dot out stocking yeah this one that views we want to import obtain earth token so what this is going to do it we don't need to create a new view in our views of ui so this is a default or built-in view which is from the restframework.org token so it allows us to just send the user's credentials and then it obtains the authentication token for us so now let's add a new path let's give a comma path and then right here once the api slash token slash and then let's just give it a obtain alt token and then let's give it a name or something like obtain so now that we have that we can test our api so first of all let's copy this let's go back to postman so what we want to do is to open a new tab and then let's set this to post and then let's give it slash api token and then right here we're gonna come into body form data and then i'm gonna use the key of username it's gonna be the username which is admin and then a key of password and then this password i'm gonna leave it blank because i don't want to show any password in this video but once you just put the password of that particular user i need to send it's going to show you the authentication token for that user basically so that is how you can do that you can also access this from your code you know you can do something like sending a request to this particular page and then getting the authentication token so that is how we can easily authenticate a user in our jungles framework so i really hope you understood everything we did in this video i hope you go to the concept of the jungler's framework i hope you understood apis i hope you now know about serializers i hope you now know about authentication in your jungle rest framework project so guys that's gonna be all for this rest framework tutorial i really hope you enjoyed it and if you did please don't forget to smash the like button and subscribe
Info
Channel: Code With Tomi
Views: 6,587
Rating: undefined out of 5
Keywords: django rest framework, django, django tutorial, python django, python django tutorial, django rest framework full tutorial, django rest framework tutorial, django rest framework crash course, django rest framework full course, python, drf, drf tutorial, web development with python, django api, APIs in django, using apis in django, code with tomi, codewithtomi
Id: wv8b3tzShq4
Channel Id: undefined
Length: 63min 51sec (3831 seconds)
Published: Mon Jun 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.