Introduction - Django File Upload Tutorial - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this tutorial we are going to explore Django file uploads and then we are going to explore some examples using just the file system to save the files and handle the uploaded files and also using model forms but just before we jump into the code into the examples there's some very basic concepts on how file uploads walk with Django so first thing is that you always send the data using post request and then on the HTML form it's very important to include the proper in code type so it's a common mistake to forget it and if you don't see the file that you sent to the server it's likely that you forgot you add this Inc type property here as moot part slash form data and also you upload the files and submit it to the server it's going to be available inside the request object inside these files files is dictionary and then the keys to this dictionary is going to be the name of the file input that you added to the HTML so if they fire input is name a document and then you just you should use this key on the files object to get the uploaded file and then each of those files that we get inside the dictionary are going to be uploaded file instance that are defined in the Django core so we have here the documentation we can call for example the read method or get the name of the file size the content type so that's what we are going to use to handle the files there so first thing we want to do here is to create a basic example without model forms or anything just handling it by by hand so can see the process so maybe we can go to the project now and create a new view great of your name it upload first just spread the ring to this uploaded dot HTML template create it here stance phase phases this document here is just basic bootstrap for template only one content block name it content and put something like upload dot HTML you now we have the view the template just create a new path choose slash or upload views not upload so here when you're accessing views dot upload is actually referencing to this view function here so let's see slash upload okay first thing let's set simple form so now method posts let's include a code type so we include also them cross-site request forgery token and then you put typed file and the name test document now just hand a submit button let's see the result okay but right now we don't have any processing or anything we can start adding here so if the request dot method is a post we can get uploaded file and then request dot files we access the dictionary and then the need the name here the key is going to beat this theme here of the HTML input and then before we move forward and save it we can see here so they uploaded file instance we can just print to the console the name and the size of the file so print total file dot name and the size okay let's try to upload some file here so here we see the the file name and the size so next thing we want to do is just save in a directory here on the server but then we need to do some configurations before we do anything else so we need to set the major hood the major URL and then add some extra settings here to serve those files in our local machine because actually Django is not supposed to serve static files and measure files even though it can but it's not very efficient on doing so so what we usually do is to set apache or nginx to serve those files so by default django is not configurated to serve those files but so we don't need to set up a proper server on our local machine we can just use Django to serve it in the during the development but first thing let's set the magico 1010 media URL so let's go to settings so here is where we want Django to saved uploaded files so this can be relative path here using the the same strategy that we use with the database file and the templates or if you want you can also put like an absolute path like temp medium Django something like that so and then the media URL is how you're going to serve those files and show a little bit more about that very soon okay so with the media hood we can all read save those files but then just serve we need to do one more step over here I'll share this link with you guys on the description below and we can add this to the setting date I mean the URL dot py so what is it doing so if the settings is in the book the bug mode which is only during development then we add this special view here that serves those media URLs the media content so it's just for development purpose you should have used that in production and then we need to import here settings and this static URL okay and then now shall handle those files we can use this file system storage it's better than opening the file using Python code and saving it by hand Django have very reliable and very secure and API so we can use here this file storage class that we can import to our view so let's do it now Kengo files storage and then here you can create an an object and then we can call here just save this file I have a safe method yeah so we should pass the name of the file and then the file itself file system not save load that file dot name and then just load that file let's try it again okay to check I can go to the five exploring here look at me media so here we have the file that we just uploaded and the good thing about using these file system storage is that if you upload the same file again with the same name you don't have the risk of overriding the existing file because I already handled it for you so if we upload again we see here it append some random string to the end of the file name so it's very convenient and then here if you we want to see this file with Django we can just put here media and then the name of the file and then a very important note here is that this slash media is not related to this one this name here is actually related to these media URL this can be anything for example if I put the media URL as this so that means and save here yeah so that means if this file here is going to be available in that URL okay so if we try to assess median does not exist so here you can see what Django is actually doing with this code here so it's appending the media URL as a last URL pattern here and serving the files that are inside this directory we can also use the filesystem storage to generate a URL for us so if we wanted to just display to the user what was the file and that he or she just uploaded and put a link here or something we can grab the name of the file and then you can grab the URL by using the file system dot URL and then passing the file name that just created I could use this here should be the same name but then if there was this scenario where the user uploaded a file with a duplicated name so the file system will generate a new unique name for this file so that's why we are assigning here a new variable and then using it to generate a full URL so we can maybe first just print it here so we can see so we have here the URL to this file so we could do something like that context and then and then we send this context here to the template so we check here if you are el loaded file then we can put something like that let's just resume it the same form we have here so you can see that it created a new file name then we if we click yep and then here we can just fix the meteor URL so now we should blow the new one but then it's not very efficient especially if you need to display those files here to the authenticate the user or to the visitors or whatever but then what we are going to do next is to use model forms to handle these file uploads and store of reference in the database so we can list it and we can retrieve certain files so this is just a brief introduction we are going to build on top of that in the next tutorial [Music] you
Info
Channel: Vitor Freitas
Views: 187,744
Rating: undefined out of 5
Keywords: Django, Python, Tutorial, Django Tutorial, How to, Vitor Freitas, Simple is Better Than Complex, Django File Upload, How to upload files with Django
Id: Zx09vcYq1oc
Channel Id: undefined
Length: 17min 11sec (1031 seconds)
Published: Mon Nov 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.