Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: User input and HTML forms
Lecture: One source of data

Login or purchase this course to watch this video and the rest of the course contents.
0:00 And you seen our forms are working really well. If we log out, we can go back to our register and this data exchange is working great.
0:08 But we also saw that sometimes it comes from the form it can also come from a query string, right?
0:14 Or if we're over here, it could actually come from the URL. Now, there's data coming from all these different places
0:21 I don't really love it, and I would really like to have one place to go and just ask, like Did they submit an email?
0:27 I don't care how, did they submit it? I want to see it. Alright, we're also not talking about headers
0:32 but that's another place or way the data is passed in. So what I want to do is add a little utility class
0:38 that creates, instead of just one dictionary for the form one dictionary for, say the query string.
0:44 Just a unified place, a unified sort of merged dictionary where I can ask, What value did they put for their name? What is their email?
0:52 I don't care where it came from. Things like that. So, let's go and do that real quick. So that seems like that belongs under infrastructure.
1:01 And I'm going to call this request_dict something to that effect. Now I'm going to copy and paste some stuff cause there's that few little tricks
1:07 that are not worth seeing me recreate. So what we're going to have a factory method here called create, and it's going to take a request
1:14 and it's going to take a bunch of route arguments. Remember Flask lets you pass variables, arguments into the method.
1:22 So we would need to carry those along. We want those included in this. I want to say that first thing that we could look at is the query string.
1:29 Now, it's super hackable, you can just type in it. So that has the lowest priority. So like, for example, if the URL has a value
1:35 and the query string has it the URL is going to override that. Similarly, the form is going to take higher priority over those as well.
1:44 So basically it goes from least to highest priority. The query string, then the header, then the form values and then the URL routing mechanism.
1:52 And it's going to create the single dictionary that we can just work with and ask values about. But it's not a regular dictionary
1:58 it's this thing I'm calling a request_dictionary that I created. So that you have a cooler way to access it.
2:03 So if you had one of these, ours a request_dictionary I could say our .value and either I get a None back or I get the value I don't have to do like
2:12 a .get a quote value like this. Or the crashing way of value. I can just say .value, more like Javascript or something.
2:23 So really nice of a little addition there for us to work with, and that's what this creates. So, it's like a, you know, derive from a dictionary.
2:31 So, let's go and just look at how we might change our registration method on this and then I'll tweak the rest aside out of sight.
2:39 So over here, wouldn't instead of doing all these stuff we can say, the data is going to be a request_dict which needs to be imported, create
2:51 and what are we going to pass. We'll pass the request of plus before I start a request in fact let's make an event change to this.
2:58 So didn't even tick that. And just say, request=flask.request. Like so. Alright, so then, we would potentially pass arguments
3:14 if there weren't any, right, like coming from up here but there are none, so we just say data sort of form and .get, we just say .email
3:23 and maybe we want to set some kind of default value. Like we were here, but we could just do like this for now. And we'll just do data.password.
3:32 Now, obviously it looks nicer and cleaner right, and simpler, I think. But it's also important that, you no longer care
3:39 how this data was submitted to you. Did it come from a header? Great, you got it. Did it come from a form? Great, you got it.
3:45 So let's just make sure that I can still log in that's still not what we just changed. But we're going to log in, and if this still works
3:51 everything is great. Okay, I guess we should also do the error handling. I could log out really quick. Go log in, just try to submit nothing
3:59 some fields are missing, right, working just like before. But, now we don't have the cognitive overhead of worrying about your real data, form data
4:09 other kinds of data, query strings and so. We'll just say I need the email, give it to me. So I really love this pattern.
4:15 It's totally something I built on top of Flask so use it or don't use it. It's your call.

Talk Python's Mastodon Michael Kennedy's Mastodon