Django: Getting Started Transcripts
Chapter: Posting Data
Lecture: Creating the review book template

Login or purchase this course to watch this video and the rest of the course contents.
0:00 With the URL in place, the last thing you need is a template for the form.
0:05 There's the ugly and easy way to do things or the pretty and much more coding way.
0:11 I'm going to do the second briefly though I'll describe the ugly way. the form object has a couple of methods that spit out HTML for the form.
0:20 There's no easy way to apply any sort of styling you're stuck with the HTML Django gives you.
0:26 The hard way is Django doesn't care if you don't call that method. You can write your form by hand as long as it has the correctly
0:34 named fields and is wired to the correct URL, it'll still work. Let's go do the hard way. Once again I've been busy offscreen and there's review.html.
0:48 The form object has a dictionary inside of it called errors which will be populated with any errors in the form.
0:55 This first chunk of HTML will put an error message at the top of the page, if the form has errors in it.
1:02 Next I declare an HTML form and just like the login form, this needs the CSRF safety token.
1:11 Then I'm using bootstrap's classes to make prettier form fields The form object has an object inside of it for each field.
1:20 Each field has a label tag method that will spit out an HTML label tag. I could write this by hand but it doesn't need any extra styling so I'm just
1:27 embedding the label in place. The first input field is for the custom rating value
1:34 I'm explicitly setting the range here of min and max so the user can't easily make a mistake.
1:42 Then the listing of classes of the field changes depending on whether or not there's an error. If there's a rating errors is invalid is added to
1:50 the class list to change the styling of the field. Similarly, the value for the field changes based on the value in the form object
2:00 If this page is being viewed from a get then the rating value will be empty and so I set it to a default of five.
2:06 If the page is being viewed from a post, there will be data from the previous view of the page and hence some content in
2:12 the form rating value attributes. And then at the bottom of this first form group I have a little area that shows any error information for this field.
2:25 The second form group is similar to the first but this time it's a text area for the review text content. A quick little gotcha here.
2:33 The placeholder attribute on a text area only works if the text area is completely empty.
2:39 Make sure you don't put any spaces between the opening and closing text area tags. Those are the two fields.
2:46 So the last thing you need is to be able to submit the form. At the bottom here, there's a button that does just that.
2:54 Okay, you're almost all set the view is in place the templates written and you've got your form going.
3:00 You may recall I commented something out in the book template. This URL is now valid so I can get rid of the comment. Alright, let's go write a review.


Talk Python's Mastodon Michael Kennedy's Mastodon