Creating a CRUD API with Django-Ninja #4 - Uploading Files and API Documentation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there and welcome to this fourth video on django ninja in this video we're going to take a look at how to upload files to an api endpoint that we are creating with django ninja and you can see in the documentation here that we've got a few objects that help us with working with files we've got a file object and an uploaded file object the uploaded file object is an alias to the django upload file that you get when you upload a file to a view in djangle so we're going to see how to work with that in this video so let's get started now we want to upload a file using a django template we're going to create a form that will allow us to upload a file so we'll create a templates directory within the tracks app and within the templates we're going to create an index.html and we'll just put in test in there and what we can do now is create a view a normal jangle view which will render that template we'll call that index takes in the request and it renders the request along with that index.html template now the final step in this basic setup is we're going to render or we're going to create a url which will point to that particular view to render the template so from tracks.views we'll import the index view that we've just created and we can create a path to that and the path's going to go to index and that will point to the index function so with that set up we've got the server running here so if we go to localhost index you see that we get that text on the screen so now that we've set that up what we want to do is create a ninja endpoint and that's going to be in the api.pi which contains the crud app that we've built up until now so we're going to create another endpoint in here and it's going to be an api.post request to the upload endpoint and we'll call the function upload and as as always it will take the request as a first parameter and the second parameter to that function is going to come from the django ninja documentation i'm just going to copy this code here this takes a file and it type hints that as an uploaded file object so let's copy that in and what we can do now is first of all we'll pass on that function we need to import these constructs from django ninja so i'm going to copy this code and we can import these at the top um and if let's get rid of that so that's got the imports set up now we can write the body of this function so what we've got here is a file that we can use um and the file is something that we can read into some data so data equals file dot read now the upload file in django it has a dot read method that will read the entire file into memory so be careful that that's not a very large file you probably wouldn't want to do this in a production application that reads it into a bytes object and memory which you can decode um to a string in python so when you have that data you can do anything you want you can you can save it to the file system you can do anything you want with that data what i'm going to do is return a simple response here containing some keys and values now the name of the file is available for the file.name attribute and we can also return the data itself which we've decoded here as data now this is one way to read the file and return a response but you'd probably want to save this to the file system but we'll just go with this for now so now the last step is to build the template that is going to allow us to upload a form now we've got a text here we're going to get rid of that and i'm going to copy paste some coding this is an html form the method is post it's going to be a post request and it's going to go to this url this action attribute here and we're using the url template tag in django and the name of the url is api-1.0.0 upload now what is that that is a namespace url which now you can typically give urls names and urls.pi such as name equals index it's the third parameter to the path function however what we need to do in django ninja is we don't have that option we directly create the urls using the decorator and in urls.pi we simply reference the urls attribute so what we can do is actually pass another parameter to this decorator and that's the url name parameter and i'll say upload and this url name should match what's in index.html here namespaced by this app name which is api and that should allow us to send a post request to the upload endpoint that's referenced in this api.pi file and this is the endpoint that will then read that file and decode it read the data and return a response so let's try that out now we're going to reload the front end here and now we get a very basic html form so let's hit the browse button and let's upload the requirements.txt file and if i submit that file you see that we get a response containing the file name which is requirements.txt and we get a string version a json version of the data with the requirements as a string and that is basically what we had here the file name and the file data so that's how we take a django ninja api endpoint and we use that to read in file data that's being sent from a client now i want to show one last thing about django ninja in this tutorial django ninja offers this convenient feature it gives you automatic documentation for your endpoints so localhost 8000 api if we change the endpoint to docs we'll get automatic swagger documentation for our endpoints now this is the one we've just created it's the api slash upload one and this takes in a file that's the required field so what this gives you is data on what the request body should be what the response should be and you can actually customize this in different ways we have a very basic out of the box set up but it can be customized which is very useful let's take for example the get request that will get all the tracks in the database you can see that you have an optional query parameter in the url the title string which remember we use to filter down the data that's returned and it also tells you the structure of the response and the structure can be inferred of course from the response model let's go back to that endpoint the response model was a list of track schema objects so it can then look at the track schema and it can infer what kind of response you need to be getting from that endpoint so that's very convenient and for creating data which you typically do with a post request or a put request it tells you the required body of the post request this is what data you need to send in order to create a resource in the database so this is very convenient for your developers you can give this documentation to front-end developers and they know exactly what endpoints are available and they know what data gets sent to the endpoints and what data gets returned from the endpoints and you get this for free with django ninja very useful feature so it's worth mentioning in this video so thank you for watching this video if you enjoyed it please like and subscribe there will be some posts on the blog about this as well and we'll see you in the next video
Info
Channel: BugBytes
Views: 243
Rating: undefined out of 5
Keywords:
Id: 0asjC0yNSyA
Channel Id: undefined
Length: 7min 56sec (476 seconds)
Published: Tue Oct 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.