Django: Getting Started Transcripts
Chapter: Receiving Uploads in Django
Lecture: Adding an 'ImageField' and configuring media files

Login or purchase this course to watch this video and the rest of the course contents.
0:00 To demonstrate uploads, I'm going to add the ability to associate a picture of an author with their model object.
0:09 To do that, I'll be adding an image field to the author and configuring Django to allow the ability to upload those same files.
0:16 First off, let's edit the author model to add the image field. That's pretty much it from an object perspective. It's simply a new field.
0:33 Note that I'm allowing it to be both blank and null so that the field can be empty in forms and null in the database if I did otherwise,
0:42 the picture would be mandatory. Which in addition to being a pain for adding an author
0:46 would impact existing data in our database, because this field is allowed to be empty.
0:53 The migration process is simple and Django will set this field to empty for any existing author data.
0:59 More on this in a future chapter on database migrations. Those files that I want to upload have to go somewhere and you have to tell
1:07 Django where that is and what URL to serve them under. Let me modify settings.py, scrolling all the way to the bottom.
1:25 The media route configuration tells Django where to put uploaded files. The pattern I usually use is to go up one directory from the Django project.
1:33 Hence the parent attribute, then in a folder named uploads. Then in a folder named after the project,
1:41 I do this so that if I'm hosting multiple projects on the same server. I don't have to worry about file name clashes across the projects.
1:49 The media URL configuration tells Django what you are able to use when serving these files.
1:54 This is similar to the static URL concept you've seen earlier The next change I'm going to make is inside of alexandria.URLs
2:03 I'm going to do this so that the DEV server can serve the media files.


Talk Python's Mastodon Michael Kennedy's Mastodon