Django: Getting Started Transcripts
Chapter: Receiving Uploads in Django
Lecture: Writing an upload view
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
I'm going to add three new views. The first will be for showing a list of authors. The second for showing an author's details
0:08
and the third will be the actual image upload view. As all of this is to do with the author.
0:14
I'm going to put it inside of catalog which is where the author's model object exists So opening catalog views.py.
0:24
First off some updates to the import section of the view file. When I get to the uploads view, I'm going to need to do some file manipulation.
0:34
So up here, I've added the pathlib library. I'm also going to need the media route setting and I'm going to want the upload
0:51
to require a login. So there's a couple more imports there as well. After an upload you want to send the user somewhere.
0:59
So here is the redirect function you used in the previous chapter on a successful form upload
1:04
and the final import change is I'm going to need the author model.
1:26
The first new view here lists authors. It's pretty much as basic as a view can get query all the authors in the database ordering by their last name,
1:35
then render it in a template. The next view is for a specific author.
1:54
This is similar to book listing and book in a previous chapter. You now have corresponding author listing and author views.
2:02
Okay, now, this is what you came here for, file uploads.
2:31
Like the views you saw in the previous chapter. First off, I'm going to need to display the form associated with the upload
2:37
the get renders the form for the given author that form will have a special HTML field
2:42
for attaching a file and like any form will be submitted via http post.
3:17
This is the code for managing the post. All uploaded files are included in the request object in a dictionary called files, all caps.
3:26
Each upload field in the form has a name which is the key in the files dictionary. I've called mine author photo.
3:35
Line 42 just stores a reference to that object. Line 43 constructs the name of a path, to save this binary data into. As I mentioned before,
3:46
one way of handling name conflicts is to use a unique identifier. I'm going to put the file into the media route and instead of accepting what the
3:54
user named it, I'm going to name it. The user's ID number underscore whatever the user wanted to name it. This is actually not the best practice.
4:04
Depending on your server you might get in trouble. The user might include characters in their file name that can cause problems.
4:11
For simplicity's sake, I'm going to stick with this for now but you might want to use a fully generated file name with a unique number instead.
4:19
Lines 44 through 46 write the file to the disk on the server. Line 44 opens a new file in,
4:27
write binary mode and the four loop writes chunks of binary to the file a piece at a time.
4:34
The chunks method on the upload object has smart defaults about how big each part of the file should be.
4:40
So you're using a good buffer instead of writing, say, a single bite at a time.
4:45
Lines 48 and 49 update the author object with the path for the newly created file And then, of course,
4:53
don't forget to save after editing the model object. And then all that being done.
4:58
Line 51 uses good old redirect to send the user to the author's page so they can see the newly uploaded file.