#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: Write a view to add a new quote

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


Talk Python's Mastodon Michael Kennedy's Mastodon