- Overview
- Transcript
4.7 Using Forms and Saving to the Database
We have finally come to the last view in our application. In this view, we want to give users a form to be able to "apply" a fertilizer to their lawns. To handle this, we'll need to write a new entry to a table in the database and redirect users to another URL.
1.Introduction2 lessons, 06:19
1.1Introduction00:53
1.2What You Need05:26
2.Django App Basics4 lessons, 24:54
2.1Install Django and Create a Project05:49
2.2Create an App and Run the Web Server05:15
2.3Responding to Requests06:29
2.4Creating Models07:21
3.Using a Database With Django3 lessons, 16:56
3.1Adding Models to the Database05:28
3.2Working With the Database06:29
3.3The Admin Portal04:59
4.Building Out the Lawn Care App9 lessons, 59:19
4.1Defining the App Routes07:08
4.2Rendering HTML07:27
4.3Rendering Data in an HTML Template05:08
4.4Displaying Available Fertilizers06:15
4.5Saving Time With the Render Function02:25
4.6Handling “Not Found” Exceptions06:26
4.7Using Forms and Saving to the Database09:05
4.8Adding Some Business Logic08:54
4.9Adding Some Style06:31
5.Conclusion1 lesson, 01:06
5.1Conclusion01:06
4.7 Using Forms and Saving to the Database
As I mentioned before, what I'd like to be able to do is come into the detail page of my fertilizer. And then be able to give the user or myself the option of applying that fertilizer to the. And keeping track of that, saving that information in the database. And the way that we typically do that, as I mentioned before is via a form. So I'm going to go ahead and paste the form in here into the bottom of my fertilizer.html page. Nothing too crazy, this is a very common form and I just want to show you how the basics of how this is going to work. As well as a little bit of a nice little helper that we get to use. So as you can see, this is going to be a basic form element, not a big deal. And it's going to have all of the normal attributes that you would normally see. We have an action and we have a method. The method being post, which is how we want to actually pass this data to wherever it's going, how it's going to be submitted via post. And the action, which means, where are we going to send this to? Now, what you could do, if you wanted to, is you could say, I want to pass this to a URL. And we could build that URL, we could say, /fert/fertilizers/applied. Now, we could do that, but one of the problems with that is, what happens if that path changes over time? What happens if we update our website or application and all of a sudden now maybe we follow a different URL structure? And that becomes a problem. Well, the way that we can kind of get around that is through a nice little helper function in Django. And we can say, I want to pass this to the URL that's associated with the name of apply. Now, why does that make sense and why are we doing that? Well, if you recall back in our views, or excuse me back in our URLs, we defined all of these paths and we gave them names. So now I can define this path right here, fertilizers id apply with the name apply. And then later on if I change what that structure looks like, but keep the name apply I won't have to change any of my other code, which is actually quite nice. So now as you can see in here, we're going to pass this or post the results of this form to the apply function. And we're going to pass with it the fertilizer.id that's associated with the fertilizer that we're currently viewing. Now, we're also going to take a little bit of security precaution here, not a lot. But I just wanted to show you how to do this. But you can put in here a csrf_token, which is a cross site request forgery token. Which kind of, is it just a security precaution, so that we don't have people injecting other scripts, that's being executed on our behalf on our website? This is just a little bit of a protection against that. So I would highly recommend, including that in any sort of forms that you put on your site, whether it's on Django sites or otherwise. And then we just have a couple of inputs, I want to know how many bags are being applied. So I'm going to create an input type of number for the number of bags that we're going to put in there. And we're also going to create a label for a date, because I want the user to be able to figure out or choose what date that was applied. So I could retrofit this back to whatever and I could say, yeah, I forgot to put down some fertilizer three months ago. And I could enter that information in and everything will just be updated accordingly. And then, finally, we're going to have a submit button that's going to be Apply, so nothing too crazy here. If I go ahead and save all of that and then I go ahead and view my fertilizers, I could go to Milorganite. And now you can see here is my four bags applied. I know it's not very pretty, but we will play around with that a little bit later. But as you can see here, I could enter this information in here. And then I could say I wanted to do, I don't know, maybe three bags. And I wanted to do this and I can use the drop down here and I could say, March 4th and i could go ahead and click Apply. And it's going to send me over to my Apply endpoint which is going to be the which is showing that I'm applying fertilizer 1. Now, as you recall, I'm not doing anything right now, I'm simply displaying whichever fertilizer got me here, which is fine. So now we know that my fert is working, I am passing over here. But now, I need to do something with the data that's being passed across. So how do we do that? Well, we're going to do it fairly similarly to what we did before, as far as detail. And that I want to get my fertilizer, but they don't want to do something with it. And I want to create an application of that fertilizer, so let's go ahead and work through that. So let's start with fertilizer again, so I want to go and get that fertilizer. So we're going to say fertilizer is equal to fertilizer dot and I'm going to once again do objects. And I want to get and my primary is gonna be that fert_id again. Now, obviously, you could go ahead and do the same processes we did here and that's probably not a bad idea. But I'll go ahead and let you do that as an exercise for yourself. So I'm going to go ahead and get that fertilizer and I want to get the information from the posted data that we sent across. Now, remember, we are sending across two pieces of information bags and dates. So we want to be able to get that information out and do something with it. So I'm going to say bag is going to be equal to, and the way we get that out of that post is data is, we simply use that request that we passed in. And I want to go into the posted data, that's a built in property that I could use to get the data that was posted and I simply ask for that data by name. So I can say, give me the bags and then I can also say, the dates, I can say request.POST, I want to get the date. So now I have those two pieces of information and I want to go ahead and I want to add an application of that fertilizer. Now if you remember, in our models we created this kind of connection between fertilizer and applications. And the nice thing about doing that is so now I can go on here and I can say, all right, Fertilizer.application_set. So when you create these foreign key relationships, you get this property that's going to be the name of the associated model. So in this case, application_set and that's going to be that one to many collection that associate these things together. So then I can say .create and then I simply do the same thing that I was doing before when I was creating the Fertilizer at the command line in my shell for MySQL database. And I could just simply say, the bags applied, and date applied fields right in here. So I'll say, bags_underscore applied, it's going to be equal to bags, and I can say date_applied equal to date, just like that. Now, because I'm using this create function, I don't actually have to do a save, so that's kind of a nice thing. But if I was simply inserting, well, then I would have to do that as well. So now I have those things setup and then once I'm done with this, typically, the process that you're going to follow after a post is going to be a redirect. And I want to take the user back to the main page so that they can see what they've done. And I can actually start to see that list of fertilizer applications start to build up. So all I really want to do is I want to do a redirect. So now I'm going to get rid of this HTTP response, that's going to be the last time I use that. So I'm going to go ahead and remove that, I don't need to import that anymore. But what I do need to bring in now is an HTTP response redirect. So now what I want to do is I want to say, return an HTTPresponseredirect. And where do I want to redirect to? I want to redirect to the root of my applications, /fert, so let's go ahead and save that. Now, we're going to go backwards here and let's go ahead and just refresh this page so we can see what's going on. So now I have my page ready to go. And what I want to do now is I want to say, all right, I want to apply some milorganite to my yard. And I want to put down three bags and I'm going to put that down say, March 3rd, we'll go ahead and hit Apply. And now we get redirected back to my main list here of the applications of my fertilizer. And as you can see now, the no fertilizer available is gone, that text is gone and we just see the values right here. So we can see, what I applied, Milorganite with this name here, just like we saw, I put down three bags, three full bags on March 3rd. All right, so that's pretty cool, so let's go back to my fertilizers. And let's go ahead and do some ringer and we'll put down, let's say, 2.5 bags and we'll put that down March 19th. Go ahead, apply and it wants an integer value, so that's something that I can obviously change. So I can go ahead and hit Apply there. And so now I see I put down ringer, I put down two bags. And we'll see how much I have here, so it's actually pretty nice. We're able to now enter in that information and go ahead and populate it into our database







