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