#100DaysOfWeb in Python Transcripts
Chapter: Days 57-60: Flask Login
Lecture: What did we learn?
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
That's it, we're at the end of the chapter Flask-Login, all done. Thank you for sticking with it this long. It was a pretty technical chapter
0:09
and I'm sure there were problems along the way but hopefully you've figured them out. Let's cover off what we learnt very quickly.
0:17
This is just the technical stuff we haven't really seen before in the course and the first thing we did
0:23
was we created our database definitions, the model using Flask-SQLAlchemy. And what we did was we defined the user class
0:35
which was used to store all of our user data in and it actually uses UserMixin from Flask-Login and that contains predefined methods
0:47
that we were able to use without having to actually run through it manually and this class was the model for our SQL database. In the class, we defined
0:59
the actual database columns and attributes so we had our database.column. We're able to say it was a string, an integer for username and ID
1:09
and we're able to say whether it was nullable or unique. And in the case of the ID we're able to say it was the primary key.
1:19
And finally, we were able to create a simple class method that just returns the username. Now, while we didn't use it, I wanted to keep this here
1:28
just so that you could see the possibilities and that you can actually use it elsewhere in the program. Next, we wanted to add users to the database.
1:40
In order to do this, we had to create a user object which use the user model or class that we created in the previous slide.
1:50
And we assigned the username and the password to a user object. And this object was then actually added to the database using db.session.add.
2:04
And finally, as with most things, once it was added we needed to commit it. One little quick thing we added in as well
2:11
was Flask flash, and that was to notify that the data was entered successfully. That was just the little message
2:18
that appeared on the screen to say, hey, user created. The next thing we did that was different was to set up the actual login manager.
2:27
Now, before we even started that we need to actually link SQLAlchemy to our Flask app. Then we needed to actually create the login manager object
2:39
using LoginManager from Flask-Login, pretty simple. Then we just had to initialize it against the actual app.
2:47
So, initialize the app using the LoginManager. And then this was the specific part. We actually had to tell LoginManager what the login view was called
3:01
and this was the same name as the actual function that ran our login page where the login would be instantiated where it would be called.
3:13
Finally, we actually performed the login. To do this, we just took the actual request form check to see if there was a username sent back.
3:23
And if there was, we then query the database this is all SQLAlchemy now and we queried the database for that provided username.
3:33
If there was a username, we would then check to see if the password that was attached to that user object
3:41
in the database, the password matched what the password was that was sent back from the form. So, if you entered the password
3:50
we check it against the database. If it was successful, we would log in the user and redirect them using the redirect URL for
4:00
we'd redirect them to the PyBites dashboard. That's it. So, that was Flask-Login. I really hope you enjoyed that one.
4:09
It was one of the more technical ones you'll see but hopefully you got something out of it. Play with it, head back to the README
4:17
to just cover off some extra things you can do if you have extra time. I do anticipate this may have taken you all of the days
4:25
depending on how much time you have. Either way, it's your turn, so keep calm and code in Python.