Django Rest Framework Renderers and Responses.. Django rest framework project tutorial[11]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so in the previous ones we've actually built out our authentication and also our expenses and income cred so now I want us to start talking about the concept of renderers so at this point when a user is trying to reach us at our site you can see that we get our errors and then of course we get a status code and all that stuff but then we might have a requirement like so let's say whenever we had an error in the response we should prefix like all our responses with the keyword errors or it's say we whenever we were rendering back data let's say now we sign up a user who is actually correct so let me change this let's say we sign up a user like this one and the requirement would be to return all this in a key called data such that we can have it consistent we can have like consistent responses and let's say we want to add a demo information to these responses so we can do that using the concept of renderers alright so for us to get started with that yes the documentation for render adds so you can you guys can take a look at how you can define different types of renderers how you can handle if you the different types of data you're sending so if you're sending something that's not like JSON yeah so how you do things that goes so take some time to take a look at this documentation but what I want us to do now is actually how do those two simple scenarios so whenever we have errors we want to prefix all our responses with a key errors whenever we have direct responses and no errors which it prefix or our response with the key data so let's do that so you know 10th occasion I'm going to create a file called renderers renderers xpy okay so right here I'm going to to import vendors from risk framework so you from rest framework input vendors so I'm gonna create a custom class here this is gonna be something very odd renderer actually I'm gonna call it user renderer so this is gonna inherit from renderers but so since for us we are rendering out JSON we are going to be using a JSON renderer but then you can actually see that right here we have different types of render as we can we can take a look at so I'm gonna now bring in JSON renderer which is this one and then whenever the whenever junglers remark is returning responses there has to be this function called render so so let's bring that in so we need to override it a render so now when I ought use autocomplete you can see that the render actually takes in a couple parameters but one of the most important ones is data so this will you think of this as the response that's going to be sent back so in here let's say we wanted to change or intercept it and add some stuff and it add some keys even change the data itself will do that so they would do that is so for now we are going to think of what are going to send back as the response so I'm gonna create a vertical response and I'm gonna send it to set it to empty for now so I want to look at the data being sent so whenever we are sending data town and then we have error we actually look at this so I'm gonna add a PDB here and then we will get back to this but what I want to do now is I want to set this renderer to be used as our render event occasion up views so I'm gonna copy this so if we go to a used red py which is I need to move this one inside authentication so let me do that I'm gonna set up use to use this renderer so if you got all registered you we can actually define another property here called renderer underscore classes and this can be a two point so you can have use a renderer so we need to import it just bring it in so from renderers input use a renderer make sure that's encoded correct alright so make sure I say to PO so now that we have this if you go to our registration view and we try to register so if you come back here it's reload Oh survey is done tracing render as nobody controls okay so this is that it should be from dot renderers yeah that should restart the server good we come back and try to register so here I click try it out and use the same information you can see that we opposed in the debugger so now if we come back here because that now we can try to look at everything that's available in this button is arguments being passed down so if you look at data you can see that everything being sent down basically errors right so in here we can actually based on this data to to send down something so what I want us to do now is whenever we have errors being sent down it's quite obvious that within this data response of course there will always be many things but one of the thing that I've found to be always consistent will be having error this history in the responses being sent down so I'm going to copy that this three so whenever we have errors so this is always going to be present okay so I'm going to copy this and now if you go back to our renderer so here I'm gonna do a simple if statement so here I'm gonna do if then I'm going to use these the string details so I want to check if it's in data but I want to stringify data I want to turn data to the string so I'm gonna use STR so whenever we have this then I want to be changing the response and appending there's some case so here I'm gonna set the response to be so remain put Jason here because we are going to need to use some utilities so here I'm gonna say they're supposed to be JSON the dumps and then what I want to do is add an S key so I'm going to add arrows and then I'm going to set the value of this to be data okay so from JSON this should be input Jason not from Jason all right so otherwise we want to add data instead so here we can do an else so we can set another response to be data just now can be data and then that will mean that this will be modified and we know we need to return the new response instead of our ordinary croissants yeah we can return a response mistake though so here define a char set and set this to utf-8 all right so every ran every custom render and create you always have to remember to do that otherwise it's might not like work so if we execute you can see that we get the error ski being painted and then you can see that we get basically the errors and then the errors so this is actually good this is what you wanted so let's say we use the correct one we try to execute to this you can see that we get a response and then all the way the data is perfectly with data so which is really good which is what we want so if you enjoyed the video consider giving it a thumbs up remember to subscribe to the channel and I'll see you in the next one bye
Info
Channel: Cryce Truly
Views: 9,856
Rating: undefined out of 5
Keywords:
Id: -InaSDmAlOU
Channel Id: undefined
Length: 8min 12sec (492 seconds)
Published: Sun Jun 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.