Django: Getting Started Transcripts
Chapter: Posting Data
Lecture: HTTP GET and POST methods

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Here's a quick primer on http methods, there are actually nine different methods, each of which treats data slightly differently,
0:08 fetching it, sending it and requesting changes on the server like updates and deletes.
0:13 The two most common on the web are get to retrieve a document and post to submit a document.
0:21 In both cases, the browser connects via the http protocol to the domain in the URL and sends the rest of the URL as part of the header,
0:29 along with the method to use. Forget, the server responds with the contents of a web page and a status code.
0:36 For post, the request includes a body, the content to send to the server. The response will have a status code indicating how it went.
0:44 It can also have a pages content as well. More on that in a moment. HTML has a concept of a form, something the user can fill in and submit.
0:54 Your typical interaction with the server. Using a form uses both kinds of http methods.
1:00 You do a get to get the original page with the usually empty form and then when the user fills in the form,
1:07 the browser uses the post to send the content to the server. The response to the post may include content as well,
1:14 which is often the case if there was an error in the form, the form gets sent back down.
1:19 If the post was successful, a common practice that I'll talk about more later is for the response to include a redirect.
1:26 That's a special status code telling the browser to go somewhere new. So if the form submission is good, send a redirect. That's a 302 status code.
1:35 If the form submission is bad, then send the form back to the browser and indicate where the errors are so the user can fix them and post again.
1:44 So far the views you've written have all been assuming the get method.
1:48 The actual method used by the browser is included as an argument in the request object of the view.
1:54 You could write different views with different URLs, for getting and submitting the form but you don't have to you can use the same view
2:01 and change what the view does based on the http method. The typical pattern is get to fetch the empty form for the user, post to process
2:11 the submitted form, check if the form data is okay if so send a redirect or if the form data has a problem,
2:18 the response will include the form annotated with the error again. A subsequent post will happen when the user tries to submit again.
2:26 To help this process, Django includes tools for building and managing forms. They're a little outdated stylewise.
2:33 If you use exactly what they give you, you'll get a bit of a 1990s look but you can get around that with some extra HTML.
2:40 Let's build ourselves a review form.


Talk Python's Mastodon Michael Kennedy's Mastodon