#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: Write a view to add a new quote
0:01 Now it's time to start working with forms
0:04 to add new quotes and in the next video edit
0:07 existing quotes. So back to my views.
0:11 I'm still in my quotes app.
0:13 And back to my view.
0:16 I have a quote new function.
0:20 And just we remember if linked found new in the URL.
0:26 This is a bit more code because we have to work
0:28 with the form.
0:30 We imported the form here.
0:31 Again, we defined the form in a previous video
0:34 which was as easy as extending model form
0:38 defining a meta class, referencing
0:41 the quote model, and defining the fields.
0:44 Now we can use that form and we can use it like
0:47 form equals QuoteForm.
0:53 And I pass in the request.POST or None.
0:58 What does that mean?
1:00 Every view gets a request passed in
1:03 and if a form is submitted you typically get
1:05 the payload in either GET or POST.
1:08 We have a form that's POST so request.POST
1:12 holds all the form fields that are passed
1:15 into the view when a form is submitted.
1:19 Then generally it's very easy to work
1:20 with the form because it knows from the model
1:22 what the data types and the requirements
1:25 are of every field and because the model
1:27 form looks at Django's model to comply with that.
1:31 So I can just call if form is valid, save it.
1:40 And here we use the messages framework.
1:42 So I can pass message success, edit quote.
1:53 And again, the messages are displayed
1:55 in the main template, in the base template.
2:06 So any messages we pass from the view
2:09 will be looked here with the color.
2:11 So I have green and red-based if it's a success
2:13 or a error message.
2:15 So in this case we have a success message
2:17 but if you want to have an error message
2:20 it would use error.
2:25 And we have redirect back to quotes, quote_list.
2:32 That's where we add a quote.
2:34 If there's not a form is valid that's not
2:37 a submission so it just render the empty form.
2:43 And we're going to write a new template called form.
2:50 And I'm going to give it the form.
3:00 So when quote_new is called I instantiate a new
3:03 QuoteForm that has payload or will submit it
3:06 when the user click on the submit button off of form.
3:09 Or instantiate an empty form, which will have
3:12 the required fields as defined in forms, okay?
3:20 If there's a post submission I check if the form
3:22 is valid and save the values.
3:25 The nice thing is that form works closely
3:27 with the model so if I call form saves it actually
3:30 saves a new record to the database.
3:32 So the form actually talks directly
3:33 with the database using the model as a proxy.
3:36 That's why I always recommend to use Django's
3:37 model form because it gives all that behavior
3:40 out of the box.
3:42 At that point we know it's a success so we pass
3:45 edit quote as a success matches back to the user.
3:49 And in the base template that's styled as green.
3:52 And then we do the actual redirect to the quote
3:55 list view, which basically loads this back
3:59 to the user. So it shows all the quotes again.
4:04 If it's not a form submit it will be
4:06 an empty form which we will load in with a new
4:08 quote form HTML template, which we will write now.
4:13 So I'll go to in templates, quotes, quote form.
4:19 And again, we're going to paste this in.
4:23 So again, it extends from the base.html
4:25 template and insert its content
4:28 into the block content or make a header.
4:30 And if you have a quote variable
4:32 that's an existing one so it's an edit action.
4:35 Otherwise it's new.
4:36 So for now it's only new because we're only
4:38 going to, we're not passing in quote yet.
4:40 We only passing in form.
4:42 We will see that later when we do the edit view.
4:44 Then we have the form which has method post not yet.
4:48 And this is important.
4:49 We always should add a CSRF token and that's for security.
4:55 So Django does provide security out of the box
4:57 but you still have to define it in the template.
4:59 So always add this token.
5:02 Then the form will be comprised of a set of fields.
5:07 And to respect the muay CSS tiling I did a bit
5:11 of manual work here to make sure that every field
5:14 is wrapped in a diff with class muay text field
5:17 so the styling will be nice.
5:20 So this is the display of the form.
5:21 And as this template will serve for add
5:24 as well as edit the form can be empty
5:26 or it can be filled in with data if we're editing
5:30 an existing quote.
5:31 Then we have our buttons, so the submit button
5:34 triggers the form to post, and we have a go back
5:37 button that just goes back to the quote list.
5:41 That's it, that's my form. That's my view.
5:48 So let's try it out. Oh, that's not working yet.
5:57 I'm using a comma and this should be a dictionary
6:01 so I should use a colon.
6:05 That looks better.
6:06 So let's do some focus.
6:09 Sort and comfort should be optional.
6:11 So they should work. Awesome.
6:15 Cannot delete it yet. That's a view we have not written yet.
6:25 Loosely translated from Wigner, probably not
6:28 100% accurate but this works.
6:31 Have to edit a second quote.
6:33 Any of you see the message, edit quote?
6:36 That's coming from the base template
6:38 and it's styled green because it's a success message.
6:41 In the next video we're going to edit an existing quote.