#100DaysOfWeb in Python Transcripts
Chapter: Days 57-60: Flask Login
Lecture: Add functionality to create users
0:00 Now in order to add our user
0:03 we need to be able to talk
0:05 to that database object
0:06 that we defined, right?
0:08 So, let's import that, now.
0:11 To do that we would just add it onto the end here.
0:14 From Project Awesome, import app_db
0:18 and now we should be able to talk to it.
0:22 And we will do so in just a second.
0:24 But first we need to create this add user.
0:28 This one here.
0:29 We need to create that function.
0:32 So, this is not a route, this is simply
0:34 just a little code function.
0:36 That's going to add our user to the database.
0:38 So, we paste it, username and password up here.
0:43 So let's take those, right now we'll just
0:45 keep them the same, for simplicity sake.
0:48 And now, if you remember from our model's dot profile
0:52 we defined the user. Didn't we?
0:55 So we defined the user model.
0:57 So let's call on that now.
0:59 We'll assign that to the user object.
1:02 So, user is, assign a user class that we're
1:06 creating here. Username equals username.
1:10 Now, I know this might seem bit confusing.
1:13 But if you remember the columns that we
1:15 created in that database model
1:18 they were actually called username and password.
1:21 So, we're assigning the username object here
1:26 this variable, to the username column.
1:30 So, username column is going to take the username
1:33 that someone's entered into our page.
1:35 And likewise, the password column, is going to be
1:39 assigned the password that someone's entered in
1:41 on this page, alright?
1:44 And because of the beautiful magic of alchemy.
1:49 We don't have to really put in any special
1:51 funky stuff to add this stuff to the database.
1:55 Because we formed it within that user model.
1:58 We're keeping to those columns, right?
2:01 The idea's generated.
2:02 But this, this stuff here, we're assigning to it.
2:05 We're keeping honest, we're keeping true to it.
2:08 We just have to simply add.
2:10 So, db.session.add(use).
2:17 That's it.
2:18 That's exactly how we add data to our database.
2:22 And then we commit.
2:23 So, db.session.commit. Done.
2:28 And this will add that user to the database.
2:33 The last thing here, which I'm going to throw in
2:34 'cause it takes two seconds to explain
2:37 is we want a message to pop up on the screen
2:41 to tell someone that a user been created, right?
2:44 So, that's called flash, in Flask
2:47 we call that flash.
2:49 So let's flash a message to the screen
2:54 and obviously in order to use that, we need to
2:58 import it up here.
3:00 So import flash from flask.
3:03 And after doing that, before we forget
3:06 is we actually need to import that user.
3:09 So the actual user class that we call down here
3:12 we need to import that from our model's file.
3:15 So I'm going to throw that down on a new line here.
3:18 From Project Awesome dot models, import user.
3:25 And now we can actually use that and that will work.
3:29 So let's pop over as well, to our create user
3:33 template and just for the sake of this flash
3:36 at the top, we're going to add a little bit of
3:40 code there that will take the fact
3:43 that we've added a user to the database
3:45 and actually flash it to the screen.
3:48 And I'll show you that in just a sec.
3:50 Let's move over to the file now.
3:58 And this code is actually going to go within
4:01 our head tag.
4:02 The actual flashing of that successful addition
4:06 will show up in the header.
4:08 And to do that we need to enter in some
4:11 funky Flask code.
4:13 Let's do that now.
4:14 And I will explain it in just a second.
4:17 And there's the code.
4:19 So, what we have here looks a bit confusing.
4:22 But, just go with it.
4:25 This here is all within flash, within Flask.
4:29 So you don't actually have to define any of this.
4:31 Flash and Flask take care of it.
4:33 So what we're doing is opening up a width statement
4:35 within our actual template
4:37 and we say with messages, "Get flashed messages".
4:43 So that means any messages that are flashed to
4:46 Flask, is going to use those and it says
4:49 "If they exist".
4:51 So the very first time we load this page
4:53 they're not going to exist because no user was added.
4:56 Therefore, you'll see none of this stuff.
4:59 But if the add user function is run from our
5:02 routes apply file and it pushes the flash
5:05 off to the Flask cap, this will appear on the
5:09 refresh of the page.
5:10 So if the messages exist, let's create a class.
5:14 An ordered list class called flashes.
5:17 And for the messages in the messages
5:22 let's just have a little for loop here
5:23 that lists the mount in the list tag.
5:26 So, if you have more than one message that
5:30 will return to the page, they'll all appear here
5:33 one by one under each other.
5:35 Just like a normal for loop.
5:36 So form message in messages, let's print the message
5:40 and then we're going to end the for loop
5:42 end the if and end if.