Django: Getting Started Transcripts
Chapter: Receiving Uploads in Django
Lecture: Upload view template
Login or
purchase this course
to watch this video and the rest of the course contents.
0:04
First the author listing. I'm keeping it pretty simple here. The view sends in a list of authors in the context and I loop through them
0:13
providing a link to the author's corresponding view and their last name. Note that I'm using the named URL that I just created for the author.
0:23
Clicking on that link will take the user to the author's bio page which is in author.html. This is the author specific template.
0:34
The top block here contains the author's bio information, Then there's a paragraph containing their picture, if there is one.
0:43
Note how I'm accessing the photo on line 11. The author's image field is named picture. That field has an attribute called URL,
0:53
that returns the URL used to access the file. There are other attributes as well, like what the file is named if you want that.
1:01
I've applied a bit of styling to make sure that the image doesn't overwhelm the page by setting max height to 250 pixels. Inside of the else block,
1:11
if there currently isn't a photo and the user is authenticated. I provide an upload link. This uses the URL tag to look up the upload author photo URL
1:21
and renders the length as a bootstrap button. The chunk at the bottom uses the book set query manager for the author object and
1:31
loops through all the author's books, listing them and providing links to the book detail pages. One last template for uploading the author's photo.
1:43
This is the one you came here for the upload form. Nothing much different here, the only thing you have to do is make sure to
1:49
use the right kind of HTML form field. That's the file input field type shown here on line 16 and 17,
1:58
two things you need to make sure you get right here. The name and the ID arguments have to match up to what you're expecting in your upload view.
2:07
You'll recall that in the view the requests.files dictionary key was called author photo. So here in the HTML form you need a name field
2:17
that is the same. And depending on the other tools you're using, it's good practice to also have an ID field.
2:25
Quite honestly, I forget when you must have both and when you only need the name field and rather than look it up every time,
2:30
I've just gotten the habit of providing both of them every time. Unfortunately, bootstrap does not provide a stylized upload button.
2:39
The default one is very 1990s and kind of hideous. There are loads and loads of javascript tools out there to make this prettier and do
2:47
things like Dragon drop support from your file Explorer. But for the purpose of learning about uploads, I'm going to keep it simple. Ugly but simple.
2:57
I'm going to make one last change which is an edit inside of home.html. I added a link to the author's listing on the homepage
3:09
Just to make it easy to find. Let's go try this out.