- Overview
- Transcript
3.5 Handle POST Data
Presenting a form is a great first step. But how do we handle the data coming back when the user clicks the submit button? I'll show you how to handle form POST data in this lesson.
1.Introduction3 lessons, 14:42
1.1Introduction00:58
1.2Prerequisites04:14
1.3Set Up the Development Environment09:30
2.Foundations6 lessons, 47:44
2.1Creating Models09:20
2.2Migrations and Administration08:04
2.3Create Router URLs10:25
2.4Our First View04:48
2.5Create a Template05:46
2.6Adding Dynamic Data09:21
3.Add Structure6 lessons, 52:54
3.1Incorporate Bootstrap06:10
3.2Add a Feeds List Page11:11
3.3Add a Base Template07:10
3.4Create a Feed Form09:28
3.5Handle POST Data06:07
3.6Adding a Real Feed12:48
4.Clean Up3 lessons, 16:58
4.1Give Models a Meaningful Name04:24
4.2Format the Articles Page08:49
4.3Check for Duplicate Data03:45
5.Conclusion1 lesson, 01:23
5.1Goodbye01:23
3.5 Handle POST Data
To this point, we've done fairly well. We've been able to create a new page that's going to allow us to add in a new feed, or at least, specify the URL to a new feed. But what you're gonna see here is if I post in a value URL and I hit save, nothing seems to be happening. And that's really for a very specific reason. If I were to head back over to my text editor, and I come into my views. What's happening here, every time I hit save and my form is being submitted back to the server, it is coming back into the same view that it was sent from originally, which is this new _ feed. And as you see here, we're simply, every time we come in to this viewer creating a new form, and we're rendering it out via the new_feed template. And that's we're all doing. So we're not actually doing anything with the data that's being posted back to us. And that's what we need to take care of in this lesson. Now for the time being, there is a couple of interesting things that you're gonna need to do to handle this scenario. What I'm going to do now is I'm going to show you how to differentiate within a single view whether or not this is coming from a git request that was issued by the browser or if it was a post that was being submitted via a form. So it's actually quite simple. We're gonna use this past in request object, to be able to figure that out. So the first thing that I'm going to do is I'm gonna say if request.method is equal to POST. So if this has been posted back to us, then I want to handle this a little bit differently than if it was a git request. So I'm gonna have my else block here, so else I want to go ahead and do this. So within this if, if it was posted back, what I can do now is I can transform or I can start to get the data that was passed back to me and put it back into this new model, or this new form model that I sent out originally. So I'll say my form is gonna be equal to a feed form. And then I can pass into that the post data that was sent back to us. So within this post, you're gonna find all the information that is part of that form being posted back to us. And then Django is going to so its best to coerce this into a new feed form, and give that back to us. So why go back into the form, and not just go directly to the feed? Well, we have to be able to allot for the fact that it might be possible that this is missing data. Or maybe that the form that was submitted was not valid. So that's the first thing that I'm gonna check. I'll say if form Form.is_valid. So I have a nice little is valid function here. And if it is valid at this point now I can say I wanna get the feed that is associated with that form. Now the way that I do this is a little bit counter intuitive I think, but I'm gonna take this form and I'm gonna call the save function on it. But before I do that, I wanna make sure that I specify that I want to have this commit be equal to false. And the reason that I'm doing this is because there's usually several times.,or a lot of different scenarios where you're not presenting all the bits and pieces of information in that form to the end user. You might be hiding some information, maybe some dates that you might wanna work with, or maybe there's some fields that you wanna set based on that information. So you're not presenting everything. So I'm gonna say I don't wanna commit this to the database. I simply want to get the output from this form into my feed model that was ultimately what that form was based on. Now in here I can go ahead and I can set a bunch of fields. So I can set some fields so I could do all sorts of different things in here, but for the time being I just want to modify one thing. I'm going to say feed.title is going to be equal to Title. Now this doesn't obviously make a lot of sense and you're just going to see why I'm doing this for the time being. But we're gonna update this with some more meaningful information in an upcoming lesson. So now that I've done that, now that I've made all these modifications. And I can change as many of these fields as I want to. I'm now gonna say feed.save. And now this is going to commit all the changes and all the information about this feed into my database on the back end. Now, once this is all done, if I were to just leave it like this, the user is just gonna continue to see that form and there's not gonna be anything there. So what I wanna do now is I wanna redirect the user back to the feeds list so they can see now all the feeds that are associated, that are In this application. So I'm gonna once again use some built in functionality. I'm gonna say from django.shortcuts I want to import redirect. And redirect is exactly what it sounds like. It's going to let me redirect the user. Some place within my application. In this point, I'm gonna actually redirect them to a very specific place, a view actually, the feeds list view. So, I'm gonna say, I want to return redirect. I wanna redirect the user to news.views.feeds_list like that. So, let's go ahead and save that. So now I'm handling both the post-version of the requests coming in, as well as anything else. So in this case I could make sure that everything is saved, I'll come back to here. So I'm actually gonna paste in here the actual RSS feed location of the Django project. And then I'm gonna hit save. And now as you see here, it has redirected me back to News Feeds. And as you can see here now, I have Title in here as well. So that's why I stuck that in there, just so you could see something. Cuz remember, it's technically not gonna show up here until it's ready to be viewable. So we're gonna have to make those types of changes as well. But for now we're able to go in and create new feeds. So that's a very nice thing. So in the next lesson I'm going to show you. How we're going to be able to get all the article information about that feed, without actually having to write all of that code all by yourself.