#100DaysOfWeb in Python Transcripts
Chapter: Days 57-60: Flask Login
Lecture: Add functionality to create users

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


Talk Python's Mastodon Michael Kennedy's Mastodon