Django: Getting Started Transcripts
Chapter: Course conclusion
Lecture: Forms and uploads

Login or purchase this course to watch this video and the rest of the course contents.
0:00 HTML uses the form tag to give users a way of sending information to the server.
0:05 Django provides a series of tools to help generate and manage these forms.
0:11 Oftentimes what the user is doing is interacting with one of your data objects. For example submitting a book review.
0:18 This is so common that Django gives you a class that can turn a model object into a form with just a few lines of code.
0:24 This form object can then be rendered in your templates, saving you from writing a lot of extra HTML. To interact with the user via a form.
0:33 You use two different http methods get and post. You first use get to render the page with the four minute, which usually starts out empty.
0:43 And then when the user submits the form, the same view can detect that the http method was post and process the form.
0:50 It first needs to validate the form and if the form was valid you do whatever you need to do, like save some data,
0:57 then redirect the user to another page. If the form was invalid, then you can re render the same form augmented with
1:04 error data, telling the user what they need to fix. HTML forms, not only provide a way of sending text data but properly configured can
1:12 also allow the user to send a file. This is done through the inclusion of binary data in the http post method.
1:20 Django calls these uploaded files, media and handles them specially. You typically write anything uploaded to disk.
1:27 The file name on the disk doesn't have to be the same as what the user named it. In fact, it's best practice to not name it the same
1:34 to prevent users from accidentally overwriting each other's files or doing even more malicious things. By default, all media files are public.
1:43 If the user knows the URL, they can get the file. There are ways around this, but it requires extra code. Django isn't optimized to send files.
1:52 So in production mode, you really should have your web server be responsible for the media files.


Talk Python's Mastodon Michael Kennedy's Mastodon