Flask Tutorial #7 - Using SQLAlchemy Database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody and welcome back to life last tutorial so in this video what we're gonna be doing is talking about databases and how we can actually save user specific information now I know we haven't gone into user authentication yet and talked to a password hashing and all of that and we're not gonna do that in this specific tutorial of probably the next one either but if you guys do want to learn how to do that let me know and maybe I'll make a video on that later but right now what we're gonna do is just work on saving information a database understand how that works creating some database models this will probably take you know two videos maybe three depending on how much we end up doing so if I don't get to everything in this video don't worry it's coming and the next one that one should be out soon alright so let's go ahead and get started so what I want to actually do is make it so that when a user logs in they're brought to a page that's almost kind of like a profile page like a very simple profile page where they can modify some information about themselves now to keep things simple we're just gonna make that information an email so each user has an email when they go there they can change their email they can update it they can delete the email they can do it ever they want and we'll save that in a database and then the next time that the user logs in we'll look for that email and we'll display it and then they can change it just give you an idea of how we have persistent information now we're gonna do this using something called SQL alchemy I believe that's how you say that so we're gonna start by actually just installing that I gotta stop this server so we're gonna go to command prompt and just do a pip install flask - SQL and then alchemy I believe is how you spell that so I already have that installed you guys run and enter on that again flask SQL alchemy and then we'll be good to go so we're gonna start by guest just importing that so we'll say import SQL alchemy which is gonna be our import and now we're actually just gonna work on some of the front-end stuff so getting that form set up grabbing some information from the form with a post request and then we'll get into the database so I'm gonna go to this user dot HTML file you can see I've cleared out what we had previously that showed you know welcome user because what I'm gonna put in here now is just a form that shows the users email so to do that we've done this before we're gonna say form I'm gonna say action equals just a pound one more time we'll say method equals post like that can end our form and then we'll put our input so we're gonna put a did not mean to do that we'll say input type equals email like that we're going to need a value which we'll do in a second but we also need a name so say name equals email we're gonna say what is it placeholder equals in this case which is going to be what's our placeholder B enter email and then we're gonna have a value which is going to be equal to this is gonna be kind of weird because we'll see how this works in a second email if email now the reason I'm doing this is because we're gonna pass a variable to this field or to this or a template that's called email so I'm going to check if email has a value and if it does we're gonna display that as the value for this email rather than displaying the placeholder on enter email so only the first time before the users typed anything in which will the placeholder otherwise we're gonna show the actual value of their email that we grabbed from the database which we'll do later so now let's make a submit button so say input type equals submit like that and we'll say value equals the same thing which is submit and I apologize for the sloppy typing all right so there we go we have our form in here now what else did I want to do oh I've actually already added this but let's just go over it in my base template now I've just added this thing that says div class equals container fluid this is a bootstrap class and I've just put my blog content in there so it gets off the left wall of our website just a little bit it just is a fluid container that covers the entire web page essentially I'm not gonna explain really what that is because I'm not a pro on it but it just makes it look a little bit nicer okay so now that we've done that what I'm gonna do is actually start doing some stuff with sessions and saving the users email in a session and then once we have it in a session we can kind of compare the difference between a session and a database and since we already know about sessions we'll get started with that so the first thing I'm going to do is change my user page here to have a method so it's gonna have methods I believe it's like that and we're gonna have post and get just like our login page before and now what I'm gonna do is actually inside my user function I'm gonna start setting up a bit of code and I'll walk through it after so I'm gonna start by saying oops not on all capitals and spelled correctly email equals none and now what I'm gonna do is check the current method if the users in session so essentially if we have a user if we've logged in now I'm gonna check what method we reach this page with if it was a poster if it was a get so I'm gonna say if I believe this is gonna be a request dot method equals equals post then what I'm gonna do is grab the email from that email field so to do that we're going to say email equals request dot form and that we remember we just use this key so we say email like that awesome so grab that email and then what I'm gonna do is actually store this in a session so I'm gonna say session we'll use the same key as an email equals email like that okay awesome so we're actually going to get rid of user equals user here I'm going to just do is say email equals email so now essentially what we're gonna do is we're going to pass in an email here and display that now what am I gonna say though is if the request dot method is not post so if it's a get request what I'm gonna do is get the email from the session so I'm gonna say email equals session email like that and we'll start by saying if the key email is in session now since we've already defined email equals not up here we shouldn't have any issues passing this email in and essentially if we pass a none email then we just won't display anything and we'll have that kind of template text that we talked about before that placeholder text okay so that's a bit about it for the session I'm just gonna pop the session when we log out to now so session dot pop email none just make sure we remove that when the user logs out so no one else sees that or the user doesn't see it when they log back in and yeah that should be about it so let's test this and we'll get into the database stuff okay so I'm gonna run this so python tutorial 7 pi I've already got this up here it's refresh you are not logged in all right so let's go Tim submit there we go login successful okay so I'm just entered my email here I just had to fast forward through that because I didn't want you guys to see all my emails because it was showing it so now if I click submit you can see that it submits that and our email is safe now we should probably show some message to the user to tell them that their emails saved so let's actually do that in flash a message in here so when they hit that submit button on the post request will say flash and will say email was saved like that okay awesome so let's go back in here and actually change this now let's just change that to ca submit we get email was saved okay so let's go to the home page and make sure that our session was working so go to the home page go back to slash login you can see that it still has this saved tam at gmail.com and then obviously if we close a web browser and this wasn't a permanent session this will go away and it won't be saved so now it's time to talk about databases okay so we've done all that that's all great and now let's start setting up a database you start by changing this import statement because I just realized this is wrong what our import really should be and I wasn't even close is flask under underscore SQL alchemy so from flask SQL alchemy import SQL alchemy now what we're gonna do is set up a database object which is gonna be equal to a new kind of SQL database so to do this we're gonna say SQL alchemy and then inside of here what we're gonna do is actually just put the parameter app now we're gonna set up a few kind of configuration properties here for our app to define some stuff to do with the database so to do that we're gonna say app duck config and then here I'm gonna have to copy this in because I don't want to mess it up I'm actually I'll just copy the entire line then you guys can see it so I'm gonna say app duck config SQL alchemy underscore database underscore you are I notice that's not an L that's an I equals SQLite and then users dot sqlite3 now this right here is actually going to be the name of the table that you're gonna be referencing which we'll talk about in a second but for now we're gonna leave this as users because that's what we're gonna use and then again sqlite3 so make sure you have that I'm gonna copy in one more thing here this is optional but this is just gonna remove a warning that we get sometimes so SQL alchemy underscore track modifications equals false what this is gonna do is make it so we're not tracking all the modifications to the database believe that's correct and this just pops up and tells you know add this if you don't want a warning so I figured it will add it now so we don't see that popping up okay so now that we've done that we've actually set up the kind of database configuration and now it's time to create what we call a model which we're gonna store information it so the reason why I'm showing you guys this SQL alchemy is because it makes it way easier to save information because we can write all our database stuff in Python code rather than writing SQL queries so some of you may be familiar with SQL maybe you've use SQLite 3 before this is gonna be a little bit different syntax but you should hopefully be able to understand it so any kind of object that we want to represent or any pieces of information can be stored in rows and columns in our database now the columns are gonna represent pieces of information and rows are gonna represent individual items now in our case we want to store users and our users are gonna have in this case just a name and an email and that's all we want to store so what I'm gonna do is actually define a class which is gonna represent this user object in our database and it's gonna be called users now what I'm gonna do in here I think I'm gonna actually look at this because I forget what it is is say DB dot model as the inheritance which means this is actually gonna be a data based model which means it has a few methods and things that are inherited from it now what we actually do to define the properties that we're gonna save is write them as class attributes now this is pretty straightforward you can pretty much copy kind of the lines I'm doing and tweak their names if you want to store more in or different information but the first thing I'm gonna do is say name extra sorry not name underscore ID equals and in this case we're going to say DB dot column which again is representing the type of information and then here we're gonna put what this is gonna be so we're gonna say DB dot integer like this which is gonna be the type of information we're gonna put the name actually before that which is gonna be ID and then what we're gonna do is say primary underscore key equals true now what I've done here and this is a little bit confusing so I'm gonna walk you through it in case you don't know what SQL is or how this works is every single object that we have in our database needs to have a unique identification now that identification could be a string it could be a boolean so we only have two values it could be an integer it could be whatever it wants but it needs to be unique so what I'm doing here is saying them each object in our model is gonna have an ID it's gonna have its this is going to be an integer ID and a pry keek was true which means that this is the way that we're gonna reference all these objects and all of these every single one of these keys needs to be unique in our database or in our table so in this users table is what we call it every single row has to have a different ID and that's so that we can like tell the difference between due to two different objects because if we don't have that then technically we could add duplicate elements which will result in errors when we're trying to grab elements from the table because we'll have two of the exactly the same thing so we don't know which one to return right okay so I've explained that hopefully that makes sense if you have questions leave a comment but now what we're gonna do is define our name columns we're gonna say name equals DB column name let's say DB dot string and then here we're gonna put the length or the maximum length of the string that we want to store in this case I'm just gonna say 100 characters and I believe that's actually all we need and then we're gonna say what was the last one I want to email equals DB dot column and we'll say email and then again DB string a hundred okay so let me check to make sure I did that correct I did awesome awesome awesome I Tricia Lee we don't even need to define these names here because I don't do this if I don't define the name then what's actually gonna happen is it will just use this variable name which is fine that's all we need to do so we just need to find the type here so if you wanted to change this and add more attributes all you would do is literally just take this line so email paste it you could change this to an integer string I believe there's floats bullying's they have some other values as well but you guys don't have to look those up if you want the specific ones I'm not gonna walk through all of them and then that'll say this is a new column in our database and we can store that information for each user okay so now we're gonna define an it method and this init method is gonna take the variables that we need to create a new object because technically we can store some values in here that are gonna be none values or no values which means that for some objects they might not actually have a value for that property for example say we have you know gender as an option and you know now some people decide not to declare that so maybe we leave that as none and then for some people we have male and we have female but some people just don't have for that column so what I'm going to do here now is say name an email because these are the two things that we need every time we define a new user object this ID will actually be automatically created for us if we don't do that because it's a primary key and now what I'm going to say is just self dot name equals name and self dot email equals email okay so I've tried to explain this model hopefully this makes sense again you guys can always look up the documentation and read through all this if there's anything that I skim through I don't go into enough detail but okay so now that we've done that what I'm going to do at the bottom of my program is add something that we need which is DB dot create all now what this is gonna do is actually create this database if it doesn't already exist in our program whenever we run this application so that's important make sure you put that above the app dot run so that the app doesn't have an issue when you know that database doesn't exist and it starts running and with that we can actually start saving users
Info
Channel: Tech With Tim
Views: 230,224
Rating: undefined out of 5
Keywords: tech with tim, flask sqlalchemy, flask sqlalchemy tutorial, flask sqlite, sqlalchemy python, sqlalchemy flask, sqlalchemy flask tutorial, how to use sqlalchemy python, how to use sqlalchemy with flask, how to use sqlalchemy
Id: uZnp21fu8TQ
Channel Id: undefined
Length: 14min 16sec (856 seconds)
Published: Tue Nov 12 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.