Detailed Notes
It is time for us to take our Python and Django skills to building a full application. This is part one in a two-part series of tutorials where we build an application from scratch. This series will walk through all the steps required to build your own solution.
A Python/Django Site Monitor
The application we build is a web solution for monitoring web sites and API end points. We can configure the application to simply do a heartbeat check or validate the content returned. This first part of the series focuses on the database, design, and front end pieces of the application. The "guts" of doing the checks and scheduling them is covered in part two. The bulk of our tutorial covers the Django steps, but there is plenty of Python to learn as well.
Transcript Text
[Music] so starting into this one um i do this this is a python django example i want to build an application when all is said and done that is a essentially a standalone that you could sit down walk through this code have a pretty good framework for an application and then extend it as you need i'm going to try to make something that is moderately useful for everybody and uh something that you can get there are better tools for it but if you want a quick and dirty solution then this would be it um what we're going to talk about today i'm going to get as far as i can i don't think we're going to get really into implementing all these pieces but we'll talk about them so this is from scratch we're going to create our application uh and talk about getting that in the database i'm not going to code as i go too much i'm really because it is a somewhat fair amount of code it's probably a couple hours worth of code that was that we're going to look over but i'm going to walk through it and talk about some of the key uh things that you know decisions and how we get there and then of course although i don't have it right now uh afterwards i will put it out in having a public github somewhere so that uh you guys can take a look at you pull it down and play with the source code to your desire so we'll create the application focus on the database model in the background set up our screens tie the screens to the model and then we'll talk a little bit about the backend process but we're really gonna those last couple items we're really gonna flesh those out more in another session we're just gonna try to set the table where we can for some of this this time around so start with the application the database um we're going to call this monitor and uh it's name of the application uh you can call it whatever you want but uh what we're going to do i'm not going to show this i'll show you sort of the results of it although we could i'll probably run this one real quick anyways with a different name but the way we start as and we've done this talk about this before the way we start a django application once it's installed and we have examples of that elsewhere so i'm not going to go back through that again but once you have django and python in and ready to go then you just run django dash admin start project you give it a project name and as you'll see it's going to it builds out some some structure a folder and things like that so you're off and running you're essentially ready to run an application right then you could from there start and it'll spin up a little app for you we're gonna focus in on since we're going to the the database first we're going to tweak the settings because we're going to go from the default uh sql lite that they have built in with it we're going to flip it over to a mysql database we're going to point to that we'll talk about creating the database and a database user set those guys up in the settings and then do a you know basic connection to it now stepping back the application itself is going to be well actually i think i'm going to talk about the application as i look at the model that may be a little bit easier way to look at it so let's flip over and let me do this let me take one of these and actually go with this one and i'm going to blow this up a little bit for this first little bit and pull this up a couple more times there we go so the first thing i talked about is we can do if you want to create one is you do a django admin uh start application and give it a name so i'm just going to call it monitor 2 for now oops sorry it's not start application start app notice django actually this is a nice little thing django's pretty friendly um and i've seen a couple other things like that i think ruby does this as well where it'll help you out a lot of times here did you mean and so it's like oh actually yes i did mean that so it sort of guesses its way into stuff so now uh i ran through that and if i go to monitor 2 then it just created this monitor 2 database and then it created an admin apps migrations models test views and if i look at migrations then there's there's really nothing in there but it at least starts with that very simple structure and so i actually can go in here as i mentioned uh let's see whoops i miss one here did it did i miss a phone somewhere oh wait well interesting it did not build normally it builds a manage.py and i'm not sure why i didn't get it this time let me check real quick start project oh i'm sorry it's not startup it's a project my bad so i didn't mean startup i meant to start project let's do rf monitor 2 this is why i tried not to do too much coding we talked a little bit about apps before within a you have projects then within those you can actually have application multiple applications and so at this point i've got a project and it builds by default the an app with the same name as the project but i could actually add additional things which we may look at at some point where we would do maybe a a front end a backend an api things like that but now i'm gonna keep it simple so once i've built it it's it's built a you know fairly simple couple things it's got some settings some urls not a whole lot that it's built but i can go in and i can do a run server and it's running it's going to tell me there's we've seen this for it's going to tell me if there's some database migrations because i have not set up the database but then if i go out here somewhere then i can see and i'll pull this up a bit i can see where okay it's you know i'm running my django server it's got links oops let's put this here like this a bit and it's got links so i can learn some stuff about django if i wanted to or you know for here this is just basically say hey i've got this built so now i'm going to flip over to some code here so one of the first things we want to do is we have to create our database and if i can find where i put here we go so one of the things is first thing i do is i'm going to call this database monitor and this is a little bit getting into the my sequel side of stuff but um just so you know it's creating a database is very simple you come in log into my sql say create database name the database boom done you have a database it's like any tables really of use but yeah you've got a database you can work with uh in this case i actually go in and i create a user and this is got a nice little case issue there um i created a user for this database and i'm just going to call it uh mon owner for the monitor owner and i give them a password and i need to do it i'm going to do it for whether i'm on local host or you know if it gives me if recognizes me by some sort of ip address and then i'm going to grant all the privileges and so what this does here is um creates this user mod owner and then i get all the privileges that i need to be able to administer the monitored database so if i wanted to jump over to my monitor database oh actually i can say so here so once i create it i'm going to log in as mod owner and then i have the monitor database and i'm able to start working with it so the first thing i've got to do because i'm going to not use the sql light that django provides is i have to make sure that there's a database i'm going to connect to django will not create the database that has to be there and i need to make sure i've got a user because once i have my user and password that allows me to connect to that database in order to do so i have to jump into the settings in which um let me do this real quick if i look at monitor two monitor two um there's gonna there is a settings that is already that is generated from start uh some of these files we're gonna talk about actually we're going to we create them uh they're not created from scratch but just so you notice this is one that is and most of this information is in here there's a lot of as i talked about the django friendliness there's a lot of comments around what are we doing how do we set this stuff up what do we need to do and with our settings right now the first step we're going to do is we're going to come down and we're going to find a section on database and by default let's look at this real quick default this may be a little hard to read uh but by default the engine is going to be sqlite and it's going to connect to a little sqlite database which you can see i think it actually creates it somewhere in there or maybe that's when yeah so it's already created a little uh database there the sql wipe so that you can actually work with that you can if you didn't want to use my sql you could go right in and you could start using sqlite right away we're switching gears a little bit and so what we're going to do is we're going to come in and set it up and say that we want to use the mysql backend we give it the username of what or the name of the database the name of the user we're going to connect with and the password there is as part of setup you may have to install using pip which we've talked about in the past you may have to install the mysql client we covered that in previous uh discussions so i'm not going to you know rehash that and then once we've done that then uh as that being once we've set up the database and connected to it so we can connect to it then all we need to do and i'm going to do this in the monitor two sense just to sort of see it is then we can come in and all we do is we do a python run it against manage and we can do make migrations i'm not sure if it actually and then we give it the application name or project name uh whoops oh and it's going to say no installed app because the one other thing we have to do at this point is we have to add our application to installed apps for some reason when it creates one it doesn't do this but this is going to be your name your application name so here i would have to go into the settings for monitor 2 and change it over so i'm not going to mess with that too much because i want to get too deep into the code but what we'll do is you do make migrations and so let's flip over to monitor if i can pull that code up a little nope i can't blow it oh there we go let's blow that up a little bit more so in here i can do python 3 if i spell it right python 3 manage if i do make migrations it will go through and make some but there are no changes detected because i've actually run this and then you can do migrate so in your case the first time through there's not going to be much there aren't going to be any migrations the first time we're going to make our model and then we would run make migrations and then we run migrate and it's going to up it's going to update stuff in this case i can see where there's this admin auth content types and sessions those actually all are supplied by django and then monitors my application itself and i don't have any migrations at this point so if we were to run all of these then we would be able to run our application and we wouldn't get that warning that we got way back here that said um go back here oh i'm sorry it's over here we wouldn't get the little warning here that says you have unapplied migrations so if i run it on monitor two where i just saw that i don't have uh migrations that haven't applied then i just get a straight you know it checks a couple things gets itself set up and then boom it's running so let's go over to because we're focused on the database let's flip over to the models and let's shrink that down a little bit now models is one um let me go back to here that is not included so you know when it's first built we're going to have to we'll create this models.py and it's one of these names that it's going to be looking for i think we may see it here i think there's a note on that somewhere oh no it's not in here so we're going to go in and models is basically where we're going to source our information for our database as we saw when it attempted it there was a couple different applications auth and things like that that were supported by provided by django well this is what we're going to provide with our application so models.py within an application is the model for that application so one of the things we have to do is we've got some imports we've got to do from the start and basically this is django db and the contribute models actually really you just need import models but what we're going to find is that when we start using if we use security or if we use well really it's security stuff or if we have dates we have to import some additional pieces so i have those up there but really this first one is what matters because we're going to see every one of our models if you remember this is how we do inheritance so this lkp ping or lookup ping inherits from models.model and that tells the system in the back end that we're going to be creating a database based on that now what we're going to do in this case is we have one and this is where i'm going to start talking about the application itself that we're building this time we're going to drive this off of one primary table and it's sites and what site what a site is what an entry and a site is it is a web address uh could be a site could be as far as like a website it could be an api address it's just pretty much anything that we could you know any url so it may be a straight uh address it may have a a port number it's whatever it needs to be and the reason we're going to provide that is because we are going to have that as an address where we can do some sort of a essentially a heartbeat a heartbeat or a ping or something like that of that site to see if it's up and working now the reason that we've got you know so we're talking about these columns now in these fields so each site we're just going to have which we just sort of talked about we'll have a name a description and a a link i don't have that image like that shouldn't be there i thought i got rid of that i'm sorry we're going to call it a url so we have a name and description and a url and this is so we can identify what the site is and also this address so that we can reach it now note that we have uh character fields so name and description are just they're just text you know one's uh 50 one's 250 so the name we're going to keep it sort of small and the description we're going to let it get a little bit longer and then the url we're actually making this a url field and so that tells django that it's something it's got some additional validations and things like that i'll try to get that so it doesn't pop up it's going to do some additional validation and have some basic widgets and things like that that we can use for it last check is just a date and within that we are going to that's the last time that we did a check so we're going to you know do periodic heartbeats essentially and ping is going to be um this is going to be a lookup we've got three lookup tables in each of the lookups i guess i'll jump to that real quick we're keeping very simple they just have a code a name and a description really the name is user friendly the code is essentially going to be the primary key although each of these has an id field in the background uh we're going to use this code as our primary key for looking up values and instances records within each of these lookups and we have the ping which is essentially what type of uh heartbeat are we going to do is it just a get is it a ping is it a git where we're going to actually check the value the responding value uh it could be a put you know something like that so we've got to we're going to have a couple things that we're going to be able to support we'll start with very simple and you know we'll be able to extend as we need to result is what kind of result we're looking for and this gives us essentially a type that will be easy for us to check against so that we can say whether it's a failure or not so the result could be a failure or it could be that uh maybe we got a 200 code or a 400 code or a some sort of a value return back and then we can also check that value to make sure it's the right value that was returned and then frequency has to do with how often we're going to be able to do that we're going to want to do these heartbeats we could do it a certain number of minutes a certain number of hours certain number of days and so if we look at so that's our ping is our type is how are we testing to see that what are we doing to verify that this site is alive essentially we have a desired result which is basically saying what you know what results should we be getting back it may be result value as well so maybe we get a 200 as a desired result but you know or essentially what we're going to call uh whatever we name that but there also may be a result value so for example a an api call where maybe you send it a specific call and it needs to send back a specific value or string or something like that and then uh so frequency type and frequency is you know are we going to do this we checking every minute or checking every certain number of minutes certain number of hours certain number of days and then the frequency is that number so if my frequency type is minutes and my frequency is five we're going to check every five minutes and i've just got some basic audit stuff of when the record was created and when it was last updated most of these we've covered in the past we have foreign key relationships for those lookups most of this is text we do have a couple of dates and some integers but nothing terribly interesting here we do have our url field and then all of our lookups are essentially identical um maybe note that we have the uh two string we do this because these are going to since these are going to be lookups when it does a lookup the value that we see in a selector is based off of this the string function so we're going to make sure we go off of the name as opposed to the code or the description uh description could be very a bit lengthy for a drop down uh and so and then in the the site itself if we want to list the sites we're just going to go by the name we're just going to keep that very simple so that's our model and then we can go in uh we once we've built this out then we would go in and do our make migrations and then migrate and at that point we will get a database um i don't know if this is yeah i can't blow this up too much but hopefully you can see here uh within this if i can go let's just flop i'm sorry i can't get this any bigger um but essentially what i've got here is i've got a list of tables that it generated i can see uh remember i talked about that auth application well the table names if you in case you have forgotten the table names are prefixed by the application name so these are the auth related tables the ones under uh auth underscore i've got the django related ones which is uh the admin and the session stuff and then monitor is my monitor is my app and i can see where i have my sites table my lookup result lookup ping and lookup frequency and actually since i did just delete uh i did delete a column out of this in my model let me make sure i saved it we can actually that gives us an example to play with a little bit so now i can go in if i do make migrations then it's going to go in it recognizes here that i remove that field img link from sites and then if i migrate it's going to execute that and so it applied this monitor remove sites image link and if we look over on we've got a migrations folder and we can see each of the migrations so i can see here this is my initial so i came in and it created the model for lookup uh frequency ping and result created my initial sites table and then i came in and i tweaked i think maybe i added a result value i came in here and set a couple of uh properties around a few of these fields and then i come in here and it's going to come in and remove field image link which i just did so i can actually see my entire history of changes to my database as well let's see i think let me go back and make sure and then um just for grins we'll go ahead and whoop sorry here i'm gonna run this i can find my run server line real quick there we go i'll close that out oh i'm already running it i'm sorry i'm running it over here and let's just make sure we close it and reload it and from that now i'm going to see now i've got a couple other things here but now my site is up and i want to actually flip over to the admin side remember because uh this sort of somewhat for free we've got and i'll show that in a minute um is that because we built this we can actually see the um on the admin side we can see our tables groups and users is in there by default frequencies i can see what the frequencies are that i've entered i can look at ping types i can look at result types i can see some sites that i've already done i can add those so i can do all of this through the admin but we're going to have a front end and as a reminder uh the authentication authorization that's an application that it's it's already built those into this admin side monitor i had to add them and i had to do that through admin dot py and that is one that i'm pretty sure i have to create let me double check that just to be sure uh yes i do have to create by default it does not have an admin dot py but if i want tables to show up here then i need to do this which is um pretty simple i have to import the admin from djangocontrib and that says that i'm hooking you know i'm connecting to the uh the admin app essentially and that's what that admin is and so for each of these uh and i'm going to pull my models since i did it for all of these guys so i'm going to do um lookup frequency ping result insights all four of them and then with each one of those all i'm going to do is create a class that inherits from the model admin for each of those i get a display a list of columns i'm going to display which is here on the list so i can see name and url for these it's going to be name and code and you can see that name code name code name code name url if i wanted to add another column um let's see if i think i can look up a result i think there's a field there and i can add that actually let me go look at my model real quick uh oops uh let's see well let's just now let's do less yeah let's do last check so if i do last check over on admin then i'm going to see it's going to do an update which it did and now if i look over here sites gives me the last check field as well so i can play around with that as needed but i can fairly as we've seen before i can pull these things into the admin and then all i have to do so i create the class that is essentially the admin model form based on this i mean all that behind the scenes django knows what it needs to do and then i just register i just register each of them and basically just say you know what i want to call it and or what the model is and then what the admin class essentially the form class is so i can pull those guys in and i'm ready to go so that gives me my database now i'm going to talk a bit about um we did let's see we've sort of gone into the model um i sort of skipped ahead to this a little bit but that's okay uh so just summarizing that a little bit primary table is sites table uh we've that's where we're going to list our information we've got some lookup tables that are basically just support tables for that main table to give us an easier way to enter data we do need to create a super user and we're going to talk about populating tables and fixtures next so creating a super user is uh moderately simple you just say let's see i think i have it specifically otherwise i may have to look it up because i always forget even though it is rather simple where did i put that i don't have it okay i think it is i think it is just let's do this i think it's just called yeah create super user so what i want to what i have i've already built but let's say i want to go into uh monitor and actually let's do it this way let me go there we go go back over to monitor now right now if i look in my oh i got to keep that running let me look at my users real quick right now i have a user named rob that i created so that's my only user and this user is has a staff status so it's you know basically it's a super user i can it's active and it's staff if i want to create another user so let me create i'm going to create a guest then that's createsuperuser so i've got that all i have to do is just say createsuperuser just ask me what the name is and so i'm going to call it guest i give it a email address give it a password i think it doesn't like super simple ones but let's find out yeah so if it's too kind i try to use password123 and i can it'll save you know you can bypass the validation you want to which i'm going to because i don't need to worry about it so now i will be able to look in here if i refresh whoops i got to start my server back up and then refresh and now i can see that i've got guest user um and again i can see that they're active their staff their super user but i'm going to go in and somewhere in here delete and we talked about these uh users and permissions and things like that in the past i'm not going to rehash that too much particularly because it's not going to matter too much for our application one of the things that we need to do is we're going to need to do uh enter in these lookup values to some waveform or fashion now we've got this uh this interface that allows us to you know fairly easily do so it's user friendly good user experience but i do want to mention i want to mention something we've done in the past or that we haven't really covered the past which are fixtures and those allow us to along with the data migrations we can use those to develop on one machine and those remember those migrations are sql scripts so i can go run those same migrations i can run those on another server and update it fairly quickly what migrations don't do is handle data that is where fixtures come in now what a fixture is is i can take a look at a table uh for example let's do i think results because i don't think i've done that one yet let me go look real quick in my folders where did that go here it is so i did frequencies before so for results instead of me having to update my database and go do this again i can actually do a dump and that's where fixtures come whoops let me do it this way that's where fixtures come in and so what a all i need to do with that and i'll show you what a fixture looks like i'm going to go in and i'm going to do with manage again we're using this python three manage we do a dump data we give it the application name and then we give it the table name so this time i wanted to do lookup result yeah look up result uh you can give it an indentation because it's basically for pretty printing and things like that and then i'm going to put it into the fixtures folder and i'm going to call it results.json because it's going to generate a json file so i'm going to do that for result and i'm also going to do it i did it already for frequencies but i'm going to do it for pings and then we'll take a look at those because then what i can do okay so i can i've generated these and now that's the i use dump data load data is the reverse of it and all i do with that is i just tell it what fixture i want to use i tell it the app and the table and it's very much a similar kind of command and it'll actually pull that it'll insert that information into the database so i can i'll do that in a second here so let's look at these first so let's look at results if i look at my data i had uh failure value response is alive and simple response i had these four items when i created the um the fixture then it creates just a nice little json structure for each of those records it tells me what the model is so here's my app and my table what's the primary key what are the fields and the values and then it just does that for each of those so i can also do that for ping i can see where i've got it for my ping values and i can see it for my uh frequencies as well now note that it has the model within so technically i could just combine all of these uh the data from all these tables into one shot and try to pull it in um but it's easier to keep track of doing it separately so there's some way there are some advanced things you can do if you really if you have to pull like an entire database and want to do that through django as opposed to the databases uh you know bulk load and backup and features like that but for now we'll keep it you know simple you can do it on a table by table basis and what i can do is and i'll show an example of load data so i'm going to do the frequencies so if i go into my database and look at frequencies here's my data and i'm just going to get rid of all of the records in there so i've emptied my table uh it i think it's okay with that i didn't like that for some reason let's see oh i'm sorry because this has got foreign keywords oh because i've already got a record in there so let me get rid of my site come here if i can double click properly there we go my mistake i forgot that i had we had a record there so if i come into frequency now i can take all of those get rid of oh now let's just do it this way i'm gonna come back there we go okay so now i've emptied those out but now if i wanted to come in and load it should look up frequency right right um oh probably have the wrong i think i called it look up oh what did i call the fixture let's go look at that real quick i called the fixture frequencies.json so and so now it installed four objects from one fixture and if i go back and look at my data i can see where it installed each of those i went ahead and pulled those guys in so i could do that on another database if i wanted to take this application and quickly migrate my entire database including data then i can use those fixtures they are also useful from testing and things like that there's some there's a lot of different uses for it the built-in test system actually makes use of fixtures behind the scenes so this just gives us a good way to move data back and forth so that gives us a way to create our model create our data and back it up you know move it around so that really is that second working session so now let's switch gears into the front end we're going to talk about our screens for our application our pages and they're really we're going to keep this sort of simple we're going to have a home page there's going to be some base navigational stuff uh like a base menu we've got a header and a footer for our pages we're going to talk about we're going to have a style sheet that we use for application we're going to pull some stuff in there and then we're going to have a screen that allows us to add or edit a site uh one other one going to do is we'll be we'll have a which is essentially going to be sort of the home page as well is we're going to have a list of our existing sites so to do this we're going to create some more files and fill these things out and this is where there's going to be a lot of code i'm going to go through but i'm going to try to walk through it somewhat quickly because it's it is moderately repetitive so hopefully you guys will be able to easily take a look at that when you pull down the code if you want to build your own example then it should be easy to read and modify so we've got a couple of things that are key i guess to building a page we have to have a url we have to give it an address we have a view which basically says hey when i get to this address this is what i'm going to do we're going to build an html template because we're it's sort of as a you know jumping the gun a little bit here but when we build out a view at some point we're going to display something which is going to be an html page maybe with a form within it and as part of those html pages we're going to have a template which is going to be sort of our basically we're going to be storing our header and footer things like that in there and then we're also going to talk about our style sheets so that we can give it a general look and feel let's see so we jump over here so let's start with our urls let's see we can close a couple of these down a little bit simplify a little bit let me close a couple of these off so start with our urls now urls is actually generated um you have some by default so i'm just gonna do a cat monitor two monitor two oops monitor two uh by default there's very little that it gives which is and that little is the admin site if you remember when i did fired up monitor 2 i was immediately able to flip over to the admin site and that's because it's built that slash admin if i do anything else on it then it's it's going to tell me that it can't find it basically and i think we have seen that before you will see an example somewhat like do i have that running in the background i do not let me run my server again and i'll show you an example if so if i come in and give it an address that it doesn't know what to do with it it's going to say hey so um i'll just call it mistakes if i try to go to that page mistake it's going to say page.found and it's going to show me this is what is actually defined within the url so these are the patterns that i register that i recognize now notice i'm seeing this because i have debug equal to true in my settings file if i change that to false i'm not going to see that i'm going to get a standard for a fuller page so while we're working on this while we're developing you know debug on we're going to se or equal to true you're going to get a lot of useful information this one for example i don't know how many times even building this little application where i had a typo or something like that and i could easily say oh here's what i'm looking here's what i sent here's what i you know oh wait this is what i meant to send and so i can correct that and then it'll direct me accordingly now it includes the uh admin and uh by default and that's the only uh and uh import path which is this uh the patterns we're looking to match i'm keeping them very simple you can actually get more complicated with your patterns uh you can do uh regular expressions and things like that but i'm going to keep them simple one of the things i'm going to do is from monitor my application i'm going to import views so it's going to go find views.py which we're going to go fill out here in a little bit and from the django piece i'm going to import include because that allows me for the uh the user authentication piece that i'm going to use i'm going to go ahead and include in uh accounts and then i'm going to take and i'm going to have so slash account which i could be this could be users could be a lot of different stuff but i'm going to hang off of the accounts url address all of the urls that are the auth related urls which are things like login and log out things of that nature so that's more of a security thing that's built into django so we're going to take advantage of it and then we go to our pages we have our home page which is just going to be our our default now so if we don't give it a if we just go to the site it's going to jump at our to our home page sites which is going to be our ability that's going to be a list of the sites that we've entered we're going to be able to edit a site by giving it an id we're going to add a site if we don't give it an id and then we're going to go to we're going to have a youtube user registration page and so those are our urls and so now we have home page list sites edit site ad site and user register that we have to build out in our views and so as we flip over to our views um a lot of this stuff is going to come um as we go so there's a couple of these i can actually eliminate out i don't use time zone um but i'm going to leave these in here anyways for right now and this would be stuff that again you'll get the code and you can fairly quickly i think you'll see what you need and what you don't need and like i said chango is pretty friendly so when you try to run something and it doesn't see something it's expecting it will say hey i can't find you know such and such a library which usually will imply that you then need to go and import that library you know or do it from now home let's just sort of walk through our pages from the view point of view um i'm going to come back we'll look at some of these html things here in a little bit this is the the logic behind it so for home page uh we're going to come in and we're going to see if the user is logged in if they are we're going to go to welcome.html so they're going to get a welcome page otherwise i'm sorry if they're not they're going to go to the welcome page if they're not logged in if they are it's going to go to the list of sites and notice one it's going to render so it's going to actually it's going to say all right i'm going to kick over and render the html page this one says instead of going to slash you know just slash as your home it's going to redirect me to slash sites which is going to go to the sites page so i'm going to if i start here and i'm logged in it's going to redirect me to sites so it's going to go to this list sites call and that's just behind and just within this it's going to do a quick redirect and now i come into list sites so we take a look at that one which is sort of our it's our home page for people that are logged in um if they are authenticated in this case if they're logged in so if they try to go to the sites page and are logged in then it's going to get all of my site objects or site records out of the database there's no filtering i'm going to set up a parameter of sites which is all that list of all records and then i'm going to render the site list and i'm going to send it parm the parameters which happens to be the list of all sites if i'm not logged in it's going to tell me to go log in and i can see that here i think i am logged in so if i go to sites it takes me to uh my sites if i log out and go to the same thing then it's going to take me to my welcome page and we'll take a look at how these things got built out here in just a minute so that's list sites another page i'm going to put together and notice i just kept all these secure from the start so if you're not uh let's see if you're not logged in it's going to send you to the login page so all of these um if i try to go to like site then it's going to ask me to log in if i try to go directly to any of those and then i can log in and get it so i've real quickly i mean i didn't have to really build much to this is just this accounts login if you remember that was included from the auth piece here whoop sorry accounts and then i added all of the auth urls so one of them is account slash login and so i go here and i can see that that's my login so once i go for edit site that's going to take an id this is the only one that's taking a parameter so it's going to be so if i do site slash one and this is a node this is with the slash it's not a query string parameter so for example um if i do site slash one that's going to try to get me something and it does not exist i don't have any sites right now if i do it as a query parameter that's different so i'm not saying id equals one in that case it's going to think that i didn't send anything so it's going to try to add a site because that's not a that's not a query string parameter that's an actual part of the url that we are addressing here since if i just give it a query string version then it thinks that it's this because it gave me cite plus a query string so edit site if i give it an id it's going to say it's checks to make sure if it's not not zero so if it's not a if it's an id that's not zero then it's gonna go try to find the data for that site uh we saw what happens if it doesn't you get a you know four one says hey i can't find that uh otherwise if it's a zero as an id it's going to create a blank record so which basically works identical to ad but we just i did it this way so it's a little more uh ponderous approach if we post to this site so if i'm doing an update and posting then i'm going to grab the data from my form and um i'm going to see if my form is valid and then i'm going to go ahead and save and then i'm going to once i've saved my record i'm going to go back to my list of sites and we'll take a look at this in action a little later here so i come through for edit i will create a blank form which i did here so it's from site form and we'll take a look at those in a minute i'm going to send my id across and then i'm going to have a title for the page that i'm going to use and then this is going to call siteedit.html for add site it's very similar i still call site edit basically the only difference is i just come in and create a blank form i don't ever do the here where i'm trying to grab an object from the database based on the primary key and so um two other things i went ahead and i've created a logout that i can call and the user registration user registration is so we can actually create a user within our application and that's a form that we'll take a look at we've got a registration form and there's a registration html that's just some additional administrative pieces you really don't need to log out the register or even to check the authentication you could just say i'm just going to let whoever hits this application see everything so i built these and one of the things i want to do now is go for my views is we've referenced these forms all over the place forms are the application version of the model now i've got a couple forms that i'm i'm basically stealing from django which is user creation form in user and those are so i can do the registration and the user login so that gives me a really easy way to create and administer users those pretty much you're just going to use what you you know what comes you can see there are certain fields that you can require or not you can make some tweaks to it but that's sort of outside the scope of what i want to cover this time around i do want to look at my primary form is the site form and within that it is based off the site model sites model and i'm going to give it my list of values that are going to be on my form for each of these i'm giving them with labels i'm going to give them sort of some pretty labels for them and we'll see these in a minute and then there's some widgets that i've got here to allow me to do input most are pretty simple i've got text where i'm limiting the size and i'm giving it a class this is a css class in case i wanted to play around with that um and then i've also got a text area so text input single line text area i have uh multiple rows here i've got so i'm going to do two rows of 80. it probably should actually be three rows because i have a 250 limit on that one i think and then um that's pretty much and then a number input so those are my forms and so now i can go to uh let's look at our views let's take a look at the the html side of this um there's two things one first we're going to have a we're going to add a static folder which has to be addressed in settings and that's where we're going to put uh like our javascript our css images things like that so those will all go in our static folder and then by default we get a static url and then and all we have to add is that our um where our static files directory is so i'm calling it static so it's just off of my off my base directory for my application with static this os path join is a operating system generic approach to adding combining values to give you a path for the uh the hard drive for the you know the local drive so it takes care of things like you know backslashes versus forward slashes and things of that nature and again this is stuff where you not a whole lot you need to do other than just know that it's out there and once you've got it you know once you've googled it you throw it in there and boom you know you're ready to go now within my static i pulled down the bootstrap files i think we've seen this before so i'm not going to go dig that up and this just happens to be because i'm going to use bootstrap i do although i don't need to use this blog guy i'm not actually using him anywhere so i'll just move him to trash within the css uh one of the things that bootstrap gave me is the form helpers javascript monitor css is my application and then i've got these images in case i need them now static comes in use when i go to my templates so templates is where it's going to look for html stuff so when i go to you i'm sorry a view and i look and i say you know create register.html then it's going to try to look for that within the templates folder and so i have site edit which i've created here and now with each of these so let's start with our welcome uh it extends base.html so this this is built on top of base html which i'm going to look at in a second if you remember we have blocks and so we have this content block that is going to be part of this html before the block that content block it's going to go pull in base html base html is our generic format for a page we're going to load the static files so everything that's within that static folder we load it and then a lot of this is like here static we're going to pull in our bootstrap stuff both the css and the javascript um we've got the form helpers one we've got some jquery javascript that's pulled in for bootstrap pulls in another little javascript and now we pull in at la one of the last things we do is and all of these are sort of your standard uh link for a style sheet h reference and then i'm gonna give it uh within stat uh within static css monitor gives me whoops and i just accidentally hit that pulls in my my css i create a header which i'm using this is a pretty standard bootstrap navigation in effect it gives us this little thing here which also is responsive so as i shrink it down it gives me my little hamburger menu so i do that to give me a nice little responsive interface uh let's see i get my i set up my menu items which is just basically a home and then if you're logged in uh i've actually i'm using some it's still pi uh django so i can say if the users log in if they're authenticated then i'm going to allow them to have ad site and login i'm sorry log out if they're not authenticated then they're going to be asked to log in so they just get home and log in so if i log out i just get home and log in if i log in i get home add site and log out and then i have my content block and i'm wrapping that in a div that's going to be of class content i'm going to see that with our style sheets a little bit and then i have at the bottom i have a footer which is going to be of the standard footer class and i've just got some basic text for that at the very end pull in some additional uh javascript which i think didn't already have that one bootstrap yes i did i pulled in some javascript that i actually pushed that stuff to the bottom just for speed and so it comes in at the end and then finally comes in and pulls in javascript that it needs so that gives me our base so i've got a footer i've got a header and then i've got my content so if i look at welcome there's a header up here uh this content block and then there's going to be a footer underneath it my content block i've just got i've got javascript included here which is very simple for my on clicks so i can log in and register i've got a little text and so i can see that right here uh oh i'm sorry that's actually not it this is the welcome so i can see i've got my login button i've got my register with the site got login registered the site register i did not have one of my templates set up so we're not going to look at that right now oh i'm sorry i can check i can fix that real quick it's not called external it's called base and i think that will give me all i need so now yeah so now i can do user registration so i could add a user if i wanted to uh it gives me some help for each of these what is it you know as far as your username your password and it's a very straightforward uh here let's see so i do if you guys remember uh i bring in the registration form and let's go look over here real quick for user register so i'm using this registration form which i pass in i pass the form as form is the parameter i'm going to pass that into register dot html and so i can come in on the register side and i can do a form and i can either there's three ways i can do it i can do it as a list as an unordered list as paragraphs or as table if i want to do a table i can change that real quick and it's going to be a series of rows if i do that as a registration now it looks more like a table i get to the end after the table if you remember i have to send it a csf csrf token because i'm going to be if i'm going to do data entry stuff and then i have my submit button and then i've so i've put this together and i've also got my little helpful text you can also log in if you have an account log in bam bam and so i i built my header which is the navigation i've got my footer which is down here which is also very straightforward and then this is my content now you you may see that i've got my nice little coloring for that that i picked up with my css uh monitor if you remember i used a class content for that main block and so with each of those i've just come in and i set a width which is 90 i have it centered basically because it's got a padding to the left of 5 so it just kicks it over five percent i also take the text within it and push that over five percent and then i've got my background color my forward color and then my height is just eighty percent so it just sort of limits it so it's a nice little small screen very uh you know early 90s look and it's also responsive so doesn't look too bad as it shifts stuff around now uh that is so if i want to add a site so that's my welcome site list is what i'm looking at here this one's a little more complicated um i've got an if here so if the user has a first name then it's going to say sites for that user otherwise it'll say my sites and then i've got my add a site button which just takes me to add site which is right here otherwise i'm going to say if i have let's see so if sites which is if you remember that's a list of the records from the sites table if that has some entries then it's just going to be a force of foresight insights so for each record i'm going to call it site and then i'm going to come in and build out a listing of those which we'll see that in just a second and then i do an end for if i don't have any then it's just going to say no what sites entered which i have here click on add a site to get started and we'll take a look at this second i'm i've got each of these displayed there's going to be a css class site which we're going to look at in just a second so let's create a site and this again oops let's look at that real quick create a site i'm doing a form as table and i use that site form that we use here so i'm going to see site name description test url ping type blah blah blah i'm going to see all that kind of stuff so i'm just going to it's going to display that out i set the table or the title which is either add or edit and then i've got a save changes and then note i've also got my csrf token so i don't have to worry about cross-site issues so i'm going to add a site i'm going to do it this is the dp homepage heartbeat the ping type and i just had a couple so i'm gonna do a heartbeat uh the test url is https going back slash developpreneur.com desired type is uh going to be is alive desired value don't have to worry about that because if is alive it's just going to be a response uh and i'm going to do this every five minutes and so i'm gonna that's my heartbeat check when i do save changes now i could see a list of sites uh which is whoops site list and so here this is where it's going to for each record within there it's going to do an md5 and an md1 which we can there we go as it gets smaller the 5 is actually because of the size of the screen it gets funky pretty quick but normally what i'm going to see is if i have multiple rows let me add a second site let's see i'll do my this one real quick i'll do the same thing i'm going to do a heartbeat calm i'll go straight to that page simple response well it's gonna be is alive don't have to worry about that and do that once an hour save changes so now we can see two sites together each of these i've cleaned these up a little bit and i can either edit or nope i didn't have an on click so if i want to edit one then i can come in and i can edit that page uh from this so let's take a look at from the uh the css point of view uh because that's this is something we wanna we're gonna wanna play around with a little bit i think to make our applications more useful and because of the power that uh django gives us it makes it pretty easy for us to you know clean this stuff up and well we can use css anywhere apologies um so here i'm sorry so we're going to go over our css and we want i wanted to step into this a little bit just because we haven't spent much time on it so if we look at our what did i call that i call that site so here's my class and i can do quite a bit with this as you can see this is a mother moderately lengthy css entry and i and with this i'm doing things like i'm setting the radius we can see i as i hover i get my little background it has a sort of beveled kind of look and i found the easiest way for us to play around with this stuff is actually through pretty much any of your modern browsers if you want to change a look and feel then you go up here so like here i'm selecting it's hard to see i know this is very small text but i'm selecting this item so i'm going to select a site and then over on the side i'm going to be able to actually see what is the css that's being used and i can start playing with this live so i can change my background color let's say instead of that gray i want it to be uh red yeah that's so that's pretty ugly but you know i could say instead of the text color the one that it was i want to change it to be oh i don't set the color uh but whoops let's see if i can add a color real quick uh shoot i can't because okay there we go there we go so now so if i do color uh white then i can change that um i can also change around my padding so i can go from 25 to 30. or i can drop it to nothing and see that now my padding has been moved over i can work with my margins so i've got a bottom margin here i've got a little bit of space i could get rid of that i could start cranking up the uh and within the padding so now i can like really tighten some of this kind of stuff up i could increase the font i could decrease the font uh whoops that's not decreasing the font that's decreasing the font and then the the quick way to play around this stuff is play around with it live and then you can just copy and paste and just take all of that copy it out of there flip over here and i could make that part of my css and boom i'm off and running now that that hits i know we haven't gone too deep into this but that does give us at this point uh an application that we're entering some stuff i've got some basic css we'll talk about this a little bit more uh in a future session we can enter some data and that leaves us the next time we come around actually with this with this information to build a back-end that actually is going to do these checks based on pings and things of that nature so what have we covered we went through the basic app creation uh actually basic project creation and that included creating an application with it we looked at forms we looked at our model views urls went through some nice basic stuff put our application together that way looked at setting up our css and actually including that in and it's again much like any other web application you're using the same kinds of commands it gets down to its javascript and html i talked a little bit about some troubleshooting kinds of things as far as you know being having debug turned on and being able to play around with that and using our django environment for things like um backing up our data and importing it into others um of course doing our migrations and things of that nature so questions and comments yeah i just uh i wanted to say it's all more reason i'd love to uh see if you see if i could use this um you know django to to monitor one of my apps maybe not write the code in in python but um uh you know do some monitoring and just uh basic operations what you've shown here uh in django just for how easy it is to set up that's awesome yeah it's definitely there's a as a uh an ancillary application it is actually a really solid solution for that so when you and i know you you've spent a lot of times building you know like utilities and things like that those applications and and uh little executables and that that we have to build or we don't have to but we usually end up building because we want to be more productive because there's things that we find ourselves doing all the time and we want to automate the task or simplify it a task or do it in a way that's somebody that's not us that doesn't have maybe the the deep knowledge of the core application that they can do stuff that they can you know do some administration or do some reviews or reports or things like that and uh the more i play around with the more that i find that both the python itself and because of it's so lightweight and so uh pervasive as far as environments and things like that uh and then django if you want to an interface for it it's a really good pairing for doing those kinds of applications so good point glad that you you're seeing the usage as well other questions and comments if anybody's talking they're on mute otherwise go ahead and move forward so that brings this session uh to a wrap i had hoped to get further into it but just as i started looking at it and some of the things i wanted to cover this is definitely going to have to be a multi-part session we've done we've got the bulk of it we've got actually a very we have a functional front end i think this would count for like a minimally viable product from the front end point of view uh we'll do a little bit of tweaks to it and then we'll come back and swing around to the back end uh next time around as always i want to appreciate your time thank you for you know listening in for checking this out for comments and everything else if you have any other questions or anything else that you need to contact us you can always catch us at email info developer dot com you can go out to the contact us form on developernoor.com we have a youtube channel that you can go if you go look for developpreneur from youtube then you'll find it and you can subscribe and we put stuff out uh twice a week tuesdays and thursdays right now we've been a pretty steady schedule for the last year so i'll probably maintain vimeo is not um not as frequent as what we've put out from the youtube side we more often will put things like the these mentor classes will show up at vimeo first and then eventually they'll show up on youtube and then uh if you want to keep us from the podcast point of view you can always check out the better developers podcast or develop an order podcast it goes by both names for example if you have alexa or something like that say you know alexa enable better building better developers and it'll start playing the latest episode and just some of the ways we have to communicate to you guys and ways for you to communicate back to us as we work together to try to make every developer better so go out have yourself a great day [Music] you
Transcript Segments
[Music]
so
starting into this one um
i do this this is a python django
example i want to build an application
when all is said and done
that is a essentially a standalone that
you could sit down
walk through this code have a pretty
good framework for an application and
then
extend it as you need i'm going to try
to make something that is
moderately useful for everybody
and uh something that you can get there
are better tools for it but if you want
a quick and dirty solution
then this would be it um
what we're going to talk about today i'm
going to get as far as i can
i don't think we're going to get really
into
implementing all these pieces but we'll
talk about them so
this is from scratch we're going to
create our application uh
and talk about getting that in the
database i'm not going to code as i go
too much i'm really because it is a
somewhat fair amount of code it's
probably a couple hours worth of code
that was
that we're going to look over but i'm
going to walk through it
and talk about some of the key uh things
that you know decisions and
how we get there and then of course
although i don't have it right now
uh afterwards i will put it out in
having a public github somewhere so that
uh you guys can take a look at you pull
it down and play with the source code
to your desire so we'll create the
application
focus on the database model in the
background set up our screens
tie the screens to the model and then
we'll talk a little bit about the
backend process but we're really gonna
those last
couple items we're really gonna flesh
those out more in another session we're
just gonna
try to set the table where we can for
some of this this time around
so start with the application the
database
um we're going to call this monitor
and uh it's name of the application uh
you can call it whatever you want
but uh what we're going to do i'm not
going to show this
i'll show you sort of the results of it
although we could
i'll probably run this one real quick
anyways with a different name
but the way we start as and we've done
this talk about this before the way we
start a django application
once it's installed and we have examples
of that elsewhere so i'm not going to go
back through that again
but once you have django and python in
and ready to go then you just run django
dash admin
start project you give it a project name
and as you'll see it's going to it
builds out some
some structure a folder and things like
that so you're
off and running you're essentially ready
to run an application right then you
could
from there start and it'll spin up a
little app for you
we're gonna focus in on since we're
going to the
the database first we're going to tweak
the settings because we're going to go
from the
default uh sql lite that they have built
in with it we're going to flip it over
to a
mysql database we're going to point to
that
we'll talk about creating the database
and a database user
set those guys up in the settings and
then do a you know basic connection to
it
now stepping back the application itself
is going to be well actually i think i'm
going to talk about the application as i
look at the model that may be
a little bit easier way to look at it
so let's flip over
and let me do this let me take
one of these
and actually go with this one and i'm
going to blow this up a little bit
for this first little bit
and pull this up a couple more times
there we go
so the first thing i talked about is we
can do if you want to create one is you
do a django
admin uh start application
and give it a name so i'm just going to
call it monitor 2 for now
oops sorry it's not start application
start app
notice django actually this is a nice
little thing django's pretty friendly
um and i've seen a couple other things
like that i think ruby does this as well
where it'll help you out a lot of times
here did you mean and so it's like oh
actually yes i did mean that
so it sort of guesses its way into stuff
so now
uh i ran through that and if i go to
monitor 2 then it just created this
monitor 2 database
and then it created an admin apps
migrations models test views
and if i look at migrations
then there's there's really nothing in
there but it at least starts with that
very simple structure and so i actually
can go in here
as i mentioned uh let's see
whoops i miss one here
did it
did i miss a phone somewhere
oh wait well interesting it did not
build normally it builds a manage.py
and i'm not sure why i didn't get it
this time let me check real quick
start project oh i'm sorry it's not
startup it's a project my bad
so i didn't mean startup i meant to
start project
let's do rf
monitor 2 this is why i tried not to do
too much
coding
we talked a little bit about apps before
within a you have projects then within
those you can actually have application
multiple applications and so
at this point i've got a project and it
builds by default
the an app with the same name as the
project but i could actually add
additional things which we may look at
at some point where we would do maybe a
a front end a backend an api things like
that
but now i'm gonna keep it simple so once
i've built it
it's it's built a you know fairly simple
couple things it's got some settings
some urls
not a whole lot that it's built but i
can go in and i can do
a run server
and it's running it's going to tell me
there's we've seen this for it's going
to tell me if there's some database
migrations because i have not set up the
database
but then if i go out here somewhere
then i can see and i'll pull this up a
bit i can see where
okay it's you know i'm running my django
server
it's got links oops let's put this here
like this a bit and it's got links so i
can learn some stuff about
django if i wanted to or you know for
here this is just basically say hey i've
got this built so now
i'm going to flip over to some code here
so one of the first things we want to do
is we have to create our database
and if i can find where i put here we go
so one of the things is first thing i do
is i'm going to call this
database monitor and
this is a little bit getting into the my
sequel side of stuff but
um just so you know it's creating a
database is very simple you come in log
into my sql
say create database name the database
boom done you have a database
it's like any tables really of use but
yeah you've got a database you can work
with
uh in this case i actually go in and i
create a user and this is
got a nice little case issue there um
i created a user for this database and
i'm just going to call it uh
mon owner for the monitor owner and i
give them a password
and i need to do it i'm going to do it
for whether i'm on local host
or you know if it gives me if recognizes
me by some sort of ip address
and then i'm going to grant all the
privileges and so what this does here
is um creates this user mod owner
and then i get all the privileges that i
need to be able to administer
the monitored database so if i wanted to
jump
over to
my monitor database oh actually i can
say so here
so once i create it i'm going to log in
as mod owner
and then i have the monitor database and
i'm able to start working with it
so the first thing i've got to do
because i'm going to
not use the sql light that django
provides
is i have to make sure that there's a
database i'm going to connect to django
will not create the database
that has to be there and i need to make
sure i've got a user because
once i have my user and password that
allows me to connect to that database
in order to do so i have to jump into
the settings
in which um let me do this real quick if
i look at monitor two
monitor two um there's gonna there is a
settings that is already that is
generated
from start uh some of these files we're
gonna talk about actually we're going to
we create them uh they're not created
from scratch but just
so you notice this is one that is and
most of this information
is in here there's a lot of as i talked
about the django
friendliness there's a lot of comments
around what are we doing how do we set
this stuff
up what do we need to do
and with our settings right now
the first step we're going to do is
we're going to come down and we're going
to find a section on database
and by default
let's look at this real quick
default this may be a little hard to
read
uh but by default the engine is going to
be sqlite
and it's going to connect to a little
sqlite database which you can see
i think it actually creates it somewhere
in there
or maybe that's when yeah so it's
already created a little
uh database there the sql wipe
so that you can actually work with that
you can if you didn't want to use my sql
you could go right in and you could
start using sqlite
right away we're switching gears a
little bit and so what we're going to do
is we're going to come in and set it up
and say that we want to use the mysql
backend
we give it the username of what or the
name of the database the name of the
user we're going to connect with
and the password there is as part of
setup you may have to install using pip
which we've talked about in the past you
may have to install the mysql client
we covered that in previous uh
discussions so i'm not going to
you know rehash that and then once we've
done that
then uh as that being once we've set up
the database and connected to it
so we can connect to it then all we need
to do and i'm going to do this
in the monitor two sense just to sort of
see it
is then we can come in and all we do is
we do a python run it against manage
and we can do make migrations
i'm not sure if it actually and then we
give it the application name
or project name
uh whoops oh and
it's going to say no installed app
because the one other thing we have to
do at this point is we have to add our
application to installed apps
for some reason when it creates one it
doesn't do this but this is going to be
your name
your application name so here i would
have to
go into the settings for monitor 2
and change it over so i'm not going to
mess with that too much because i want
to get too deep into the code
but what we'll do is you do make
migrations and so let's flip over to
monitor
if i can pull that code up a little nope
i can't blow it oh
there we go let's blow that up a little
bit more
so in here i can do python
3 if i spell it right
python 3
manage if i do make migrations it will
go through
and make some but there are no changes
detected because i've actually run this
and then you can do migrate so in your
case the first time through
there's not going to be much there
aren't going to be any migrations the
first time we're going to make our model
and then we would run make migrations
and then we run migrate
and it's going to up it's going to
update stuff
in this case i can see where there's
this admin auth content types and
sessions those actually all are supplied
by django and then monitors my
application itself
and i don't have any migrations at this
point
so if we were to run all of these then
we would be able to run our application
and we wouldn't get that warning that we
got
way back here that said um
go back here oh i'm sorry it's over here
we wouldn't get the little warning here
that says you have unapplied migrations
so if i run it on monitor two
where i just saw that i don't have
uh migrations that haven't applied then
i just get a straight
you know it checks a couple things gets
itself set up and then boom
it's running so let's go over to because
we're focused on the database let's flip
over to the models
and let's shrink that down a little bit
now models is one um
let me go back to here
that is not included so you know when
it's first
built we're going to have to we'll
create this models.py
and it's one of these names that it's
going to be looking for
i think we may see it here i think
there's a note on that somewhere
oh no it's not in here so we're going to
go in and models is
basically where we're going to source
our information for our database
as we saw when it attempted it there was
a couple different
applications auth and things like that
that were supported by
provided by django well this is what
we're going to provide with our
application
so models.py within an application
is the model for that application
so one of the things we have to do is
we've got some imports we've got to do
from the start
and basically this is django db and the
contribute
models actually really you just need
import models but what we're going to
find is that
when we start using if we use security
or if we use
well really it's security stuff or if we
have dates we have to import some
additional
pieces so i have those up there but
really this first one is what matters
because we're going to see every one of
our models
if you remember this is how we do
inheritance so this lkp
ping or lookup ping inherits from
models.model
and that tells the system in the back
end that we're going to be creating a
database
based on that now what we're going to do
in this case is we have one and this is
where i'm going to start talking about
the
application itself that we're building
this time we're going to drive this off
of one primary table
and it's sites and what site
what a site is what an entry and a site
is it is a web address
uh could be a site could be as far as
like a website it could be
an api address it's just pretty much
anything that we could
you know any url so it may be a straight
uh address it may have a a port number
it's whatever it needs to be and the
reason we're going to provide that
is because we are going to have that as
an address
where we can do some sort of a
essentially a heartbeat a heartbeat or a
ping or something like that
of that site to see if it's up and
working
now the reason that we've got you know
so we're
talking about these columns now in these
fields so each site we're just going to
have
which we just sort of talked about we'll
have a name a description and a a link
i don't have that image like that
shouldn't be there i thought i got rid
of that i'm sorry we're going to call it
a url
so we have a name and description and a
url and this is
so we can identify what the site is and
also
this address so that we can reach it now
note that we have uh character fields so
name and description are just
they're just text you know one's uh 50
one's 250 so the name we're going to
keep it sort of small
and the description we're going to let
it get a little bit longer
and then the url we're actually making
this a url
field and so that tells django that it's
something
it's got some additional validations and
things like that i'll try to get that so
it doesn't
pop up it's going to do some additional
validation
and have some basic widgets and things
like that that we can use for it
last check is just a date and within
that we are going to
that's the last time that we did a check
so we're going to you know do periodic
heartbeats essentially
and ping is going to be um
this is going to be a lookup we've got
three lookup tables
in each of the lookups i guess i'll jump
to that real quick we're keeping very
simple they just have a code
a name and a description really the name
is user friendly
the code is essentially going to be the
primary key
although each of these has an id field
in the background
uh we're going to use this code as our
primary key for looking up values
and instances records within each of
these lookups
and we have the ping which is
essentially what type
of uh heartbeat are we going to do is it
just a get
is it a ping is it a git where we're
going to actually check the value
the responding value uh it could be a
put you know something like that so
we've got to we're going to have a
couple things that we're going to be
able to support
we'll start with very simple and you
know we'll be able to extend as we need
to
result is what kind of result we're
looking for
and this gives us essentially a type
that will be easy for us to check
against
so that we can say whether it's a
failure or not so the result could be a
failure or it could be that uh maybe we
got a 200 code or a
400 code or a some sort of a value
return back and then we can also check
that value to make sure it's the right
value
that was returned and then frequency
has to do with how often we're going to
be able to do that we're going to want
to do these heartbeats
we could do it a certain number of
minutes a certain number of hours
certain number of days
and so if we look at so that's
our ping is our type is how are we
testing to see that what are we doing to
verify that this site is alive
essentially
we have a desired result which is
basically saying what you know what
results should we be getting back
it may be result value as well so maybe
we get a 200 as a desired
result but you know or essentially what
we're going to call
uh whatever we name that but there also
may be a result value so for example
a an api call where maybe you send it a
specific
call and it needs to send back a
specific value or string or something
like that
and then uh so frequency type and
frequency is
you know are we going to do this we
checking every minute or checking every
certain number of minutes certain number
of hours certain number of days
and then the frequency is that number so
if my frequency type is minutes and my
frequency is five
we're going to check every five minutes
and i've just got some basic audit stuff
of when the record was created and when
it was last updated
most of these we've covered in the past
we have foreign key relationships for
those lookups
most of this is text we do have a couple
of dates
and some integers but nothing terribly
interesting here
we do have our url field and then all of
our lookups are
essentially identical um maybe note that
we have the
uh two string we do this because these
are going to since these are going to be
lookups
when it does a lookup the
value that we see in a selector is based
off of this
the string function so we're going to
make sure we go off of the name
as opposed to the code or the
description uh description could be very
a bit lengthy for a drop down uh
and so and then in the the site itself
if we want to list the sites we're just
going to go by the name we're just going
to keep that very simple
so that's our model and then we can go
in
uh we once we've built this out then we
would go in and do our make migrations
and then
migrate and at that point
we will get a database um
i don't know if this is yeah i can't
blow this up too much
but hopefully you can see here
uh within this if i can go let's just
flop
i'm sorry i can't get this any bigger um
but essentially what i've got here is
i've got a list of tables that it
generated
i can see uh
remember i talked about that auth
application well the
table names if you in case you have
forgotten the table names are prefixed
by the application name
so these are the auth related tables the
ones under uh auth underscore
i've got the django related ones which
is uh the admin and the session stuff
and then monitor is my monitor is my app
and i can see where i have my sites
table
my lookup result lookup ping and lookup
frequency
and actually since i did just delete
uh i did delete a column out of this
in my model let me make sure i
saved it we can actually that gives us
an example to play with a little bit
so now i can go in if i do make
migrations
then it's going to go in it recognizes
here that i remove that field img link
from sites
and then if i migrate
it's going to execute that
and so it applied this monitor remove
sites image link
and if we look over on we've got a
migrations
folder and we can see each of the
migrations so i can see here this is my
initial
so i came in and it created the model
for lookup
uh frequency ping and result created my
initial sites table
and then i came in and i tweaked
i think maybe i added a result value
i came in here and set a couple of uh
properties around a few of these fields
and then i come in here
and it's going to come in and remove
field image link which i just did
so i can actually see my entire history
of changes to my database
as well
let's see i think let me go back and
make sure
and then um just for grins we'll go
ahead and whoop sorry
here i'm gonna run this
i can find my run server line real quick
there we go i'll close that out
oh i'm already running it i'm sorry i'm
running it over here
and let's just make sure we close it and
reload it
and from that now i'm going to see now
i've got a couple other things here but
now my site is up and i want to actually
flip over to the admin side
remember because uh this sort of
somewhat for free we've got and i'll
show that in a minute
um is that because we built this
we can actually see the um on the admin
side we can see our
tables groups and users is in there by
default
frequencies i can see what the
frequencies are that i've entered i can
look at
ping types i can look at result types
i can see some sites that i've already
done i can add those so i can do all of
this through the admin but we're going
to have a front end
and as a reminder uh
the authentication authorization that's
an application that it's it's already
built those
into this admin side monitor i had to
add them
and i had to do that through admin dot
py
and that is one that i'm pretty sure i
have to create
let me double check that just to be sure
uh yes i do have to create by default it
does not have an admin dot py
but if i want tables to show up here
then i need to do this which is um
pretty simple i have to import the admin
from djangocontrib and that
says that i'm hooking you know i'm
connecting to the uh the admin
app essentially and that's what that
admin is
and so for each of these uh and i'm
going to pull my models
since i did it for all of these guys so
i'm going to do
um lookup frequency ping result insights
all four of them
and then with each one of those all i'm
going to do is create a
class that inherits from the model admin
for each of those
i get a display a list of columns i'm
going to display
which is here on the list so i can see
name and url
for these it's going to be name and code
and you can see that name
code name code name code name url
if i wanted to add another column um
let's see if i think i can look up a
result i think there's a field there
and i can add that actually let me go
look at my model real quick
uh oops
uh let's see well let's just
now let's do less yeah let's do last
check
so if i do last check over on admin
then i'm going to see it's going to do
an update
which it did
and now if i look over here
sites gives me the last check field as
well
so i can play around with that as needed
but i can fairly as we've seen before i
can pull these things into the admin
and then all i have to do so i create
the class
that is essentially the admin model
form based on this i mean all that
behind the scenes
django knows what it needs to do and
then i just register i just register
each of them
and
basically just say you know what i want
to call it
and or what the model is and then what
the
admin class essentially the form class
is
so i can pull those guys in and i'm
ready to go so that gives me my database
now i'm going to talk a bit about
um we did let's see we've sort of gone
into the model
um i sort of skipped ahead to this a
little bit but that's okay
uh so just summarizing that a little bit
primary table is sites table uh we've
that's where
we're going to list our information
we've got some lookup tables that are
basically just
support tables for that main table to
give us an easier way to enter data
we do need to create a super user and
we're going to talk about populating
tables
and fixtures next so creating a super
user
is uh moderately simple
you just say let's see i think i have it
specifically otherwise i may have to
look it up
because i always forget even though it
is rather simple
where did i put that
i don't have it okay i think it is
i think it is just let's do this i think
it's just called yeah create super user
so what i want to what i have i've
already built
but let's say i want to go into uh
monitor and actually let's do it this
way
let me go there we go
go back over to monitor now right now
if i look in my oh i got to keep that
running
let me look at my users real quick right
now i have a user named rob
that i created so that's my only user
and this user is has a staff status
so it's you know basically it's a super
user
i can it's active and it's staff
if i want to create another user so let
me create
i'm going to create a guest then that's
createsuperuser
so i've got that all i have to do is
just say
createsuperuser just ask me what the
name is and so i'm going to call it
guest i give it a email address
give it a password i think it doesn't
like
super simple ones but let's find out
yeah so if it's too kind i try to use
password123
and i can it'll save you know you can
bypass the validation you want to which
i'm going to because
i don't need to worry about it so now i
will be able to look in here
if i refresh whoops i got to start my
server back up
and then refresh and now i can see that
i've got guest user
um and again i can see that they're
active their staff their super user
but i'm going to go in and somewhere in
here
delete
and we talked about these uh users and
permissions and things like that in the
past i'm not going to rehash that too
much
particularly because it's not going to
matter too much for our application
one of the things that we need to do is
we're going to need to
do uh enter in these lookup values to
some waveform or fashion
now we've got this uh this interface
that allows us to
you know fairly easily do so it's user
friendly good user experience
but i do want to mention i want to
mention something we've done in the past
or that we haven't really covered the
past which are fixtures
and those allow us to along with the
data migrations
we can use those to develop on one
machine
and those remember those migrations are
sql scripts
so i can go run those same migrations
i can run those on another server and
update it fairly quickly
what migrations don't do is handle data
that is where fixtures come in now what
a fixture is is i can take a look at
a table uh for example
let's do i think results because i don't
think i've done that one yet
let me go look real quick in my folders
where did that go
here it is so i did frequencies before
so
for results instead of me having to
update my database and go do this again
i can actually do
a dump and that's where fixtures come
whoops
let me do it this way that's where
fixtures come in
and so what a all i need to do with that
and i'll show you what a fixture looks
like
i'm going to go in and i'm going to do
with manage again we're using this
python three manage
we do a dump data we give it the
application name and then we give it
the table name so this time i wanted to
do lookup result
yeah look up result uh you can give it
an indentation because
it's basically for pretty printing and
things like that and then
i'm going to put it into the fixtures
folder
and i'm going to call it results.json
because it's going to generate a json
file so i'm going to do that for result
and i'm also going to do it i did it
already for frequencies but i'm going to
do it for
pings
and then we'll take a look at those
because then what i can do
okay so i can i've generated these
and now that's the i use dump data
load data is the reverse of it and all i
do with that
is i just tell it what fixture i want to
use i tell it
the app and the table and it's very much
a similar kind of command
and it'll actually pull that it'll
insert that information into the
database
so i can i'll do that in a second here
so let's
look at these first so let's look at
results
if i look at my data i had
uh failure value response is alive and
simple response i had these four items
when i created the um the fixture
then it creates just a nice little json
structure for each of those records it
tells me what the model is
so here's my app and my table what's the
primary key
what are the fields and the values and
then it just does that for each of those
so i can also do that for
ping i can see where i've got it for my
ping values
and i can see it for my uh frequencies
as well
now note that it has the model within so
technically i could just
combine all of these uh the data from
all these tables into one
shot and try to pull it in um
but it's easier to keep track of doing
it separately so
there's some way there are some advanced
things you can do if you really if you
have to pull like an entire database and
want to do that
through django as opposed to the
databases uh
you know bulk load and backup and
features like that
but for now we'll keep it you know
simple you can do it on a table by table
basis
and what i can do is and i'll show an
example of
load data so i'm going to do the
frequencies so if i go into
my database and look at frequencies
here's my data and i'm just going to get
rid of all of the records in there
so i've emptied my table
uh it i think it's okay with that
i didn't like that for some reason let's
see
oh i'm sorry because this has got
foreign keywords oh because i've already
got a record in there so let me get rid
of my site
come here if i can double click properly
there we go
my mistake i forgot that i had
we had a record there so if i come into
frequency now
i can take all of those get rid of oh
now let's just do it this way i'm gonna
come back there we go okay
so now i've emptied those out
but now if i wanted to come in and load
it
should look up frequency right right
um oh probably have the wrong
i think i called it look up oh what did
i call the fixture let's go look at that
real quick
i called the fixture
frequencies.json so
and so now it installed four objects
from one fixture and if i go back and
look at my data
i can see where it installed each of
those i went ahead and pulled those guys
in
so i could do that on another database
if i wanted to take this application
and quickly migrate my entire database
including data
then i can use those fixtures they are
also useful from testing and things like
that there's some there's a lot of
different uses for it
the built-in test system actually makes
use of fixtures behind the scenes
so this just gives us a good way to move
data
back and forth
so that gives us a way to create our
model create our data
and back it up you know move it around
so that really is that second working
session so now let's
switch gears into the front end we're
going to talk about
our screens for our application our
pages
and they're really we're going to keep
this sort of simple we're going to have
a home page
there's going to be some base
navigational stuff
uh like a base menu
we've got a header and a footer for our
pages we're going to talk about we're
going to have a style sheet that we use
for application we're going to pull some
stuff in there
and then we're going to have a screen
that allows us to add or edit a site
uh one other one going to do is we'll be
we'll have a
which is essentially going to be sort of
the home page as well is we're going to
have a list of
our existing sites
so to do this we're going to create some
more files and
fill these things out and this is where
there's going to be a lot of code i'm
going to go through but i'm going to try
to
walk through it somewhat quickly because
it's
it is moderately repetitive so hopefully
you guys will be able to
easily take a look at that when you pull
down the code
if you want to build your own example
then it should be easy to read and
modify
so we've got a couple of things that are
key i guess to building a page we have
to have a url we have to give it an
address
we have a view which basically says hey
when i get to this address
this is what i'm going to do we're going
to build an html template because we're
it's sort of as a you know jumping the
gun a little bit here but when we build
out a view at some point we're going to
display something which is going to be
an html page
maybe with a form within it
and as part of those html pages we're
going to have a template which is going
to be sort of our
basically we're going to be storing our
header and footer things like that in
there
and then we're also going to talk about
our style sheets so that we can
give it a general look and feel
let's see so we jump over here so let's
start with our urls
let's see we can close a couple of these
down a little bit simplify a little bit
let me close a couple of these off
so start with our urls now urls is
actually generated
um you have some by default
so i'm just gonna do a cat monitor two
monitor two oops monitor two
uh by default there's very little that
it gives
which is and that little is the admin
site if you remember when i did
fired up monitor 2 i was immediately
able to flip over to the admin site
and that's because it's built that slash
admin
if i do anything else on it then it's
it's going to tell me that it can't find
it basically
and i think we have seen that before you
will see
an example somewhat like do i have that
running in the background
i do not let me run my server again and
i'll show you an example if so if i come
in
and give it an address that it doesn't
know what to do with it
it's going to say hey so um i'll just
call it mistakes if i try to go to that
page mistake
it's going to say page.found and it's
going to show me this is what
is actually defined within the url so
these are the patterns that i register
that i
recognize now notice
i'm seeing this because i have debug
equal to true in my settings file if i
change that to false i'm not going to
see that i'm going to get a standard for
a fuller page
so while we're working on this while
we're developing
you know debug on we're going to se or
equal to true you're going to get a lot
of useful information
this one for example i don't know how
many times even building this little
application
where i had a typo or something like
that and i could easily
say oh here's what i'm looking here's
what i sent here's what i
you know oh wait this is what i meant to
send and so i can correct that
and then it'll direct me accordingly
now it includes the uh admin
and uh by default and that's the only uh
and uh import path which is this uh
the patterns we're looking to match i'm
keeping them very simple you can
actually get more complicated with your
patterns uh you can do
uh regular expressions and things like
that but i'm going to keep them simple
one of the things i'm going to do is
from monitor my application i'm going to
import views so it's going to go find
views.py
which we're going to go fill out here in
a little bit
and from the django piece
i'm going to import include because that
allows me
for the uh the user authentication piece
that i'm going to use
i'm going to go ahead and include in uh
accounts
and then i'm going to take and i'm going
to have so slash account which i could
be this could be users could be a lot of
different stuff but i'm going to hang
off of the accounts url address all of
the urls
that are the auth related urls which are
things like login and log out
things of that nature so that's more of
a security thing
that's built into django so we're going
to take advantage of it
and then we go to our pages we have our
home page which is just going to be
our our default now so if we don't give
it a if we just go to the
site it's going to jump at our to our
home page
sites which is going to be our ability
that's going to be a list of the sites
that we've entered
we're going to be able to edit a site by
giving it an id
we're going to add a site if we don't
give it an id and then we're going to go
to
we're going to have a youtube user
registration page
and so those are our urls and so now we
have home page
list sites edit site ad site and user
register that we have to build out
in our views and so as we flip over to
our views
um a lot of this stuff is going to come
um as we go
so there's a couple of these i can
actually eliminate out i don't use time
zone
um but i'm going to leave these in here
anyways for right now
and this would be stuff that again
you'll get the code and you can
fairly quickly i think you'll see what
you need and what you don't need
and like i said chango is pretty
friendly so when you try to run
something and it doesn't
see something it's expecting it will say
hey i can't find
you know such and such a library which
usually will imply that you then need to
go
and import that library you know or do
it from
now home let's just sort of walk through
our pages from the view point of view
um i'm going to come back we'll look at
some of these html things here in a
little bit
this is the the logic behind it so for
home page
uh we're going to come in and we're
going to see if the user is logged in
if they are we're going to go to
welcome.html
so they're going to get a welcome page
otherwise
i'm sorry if they're not they're going
to go to the welcome page if they're not
logged in
if they are it's going to go to the list
of sites
and notice one it's going to render so
it's going to
actually it's going to say all right i'm
going to kick over and render the html
page
this one says instead of going to slash
you know just slash as your home it's
going to redirect me to slash sites
which is going to go to the sites page
so i'm going to if i start here
and i'm logged in it's going to redirect
me to sites so it's going to go to this
list sites call and that's just behind
and just within this it's going to do a
quick redirect and now i come into list
sites
so we take a look at that one which is
sort of our it's our home page for
people that are logged in
um if they are authenticated in this
case if they're logged in
so if they try to go to the sites page
and are logged in
then it's going to get all of my site
objects or site records out of the
database there's no filtering
i'm going to set up a parameter of sites
which is all that list of
all records and then i'm going to render
the site list and i'm going to send it
parm the parameters which happens to be
the list of all sites if i'm not logged
in it's going to tell me to go log in
and i can see that here
i think i am logged in
so if i go to sites
it takes me to uh my sites if i log out
and go to the same thing then it's going
to take me to
my welcome page and we'll take a look at
how these things got built out
here in just a minute so that's list
sites
another page i'm going to put together
and notice i just kept all these secure
from the start so if you're not
uh let's see if you're not logged in
it's going to send you to the login page
so all of these
um if i try to go to like
site then it's going to ask me to log in
if i try to go directly to any of those
and then i can log in
and get it so i've real quickly i mean i
didn't have to
really build much to this is just this
accounts login if you remember that was
included from the auth
piece here whoop sorry accounts
and then i added all of the auth urls so
one of them is account slash login
and so i go here and i can see that
that's my login
so once i go for edit site that's going
to take
an id this is the only one that's taking
a parameter so it's going to be
so if i do site slash one and this is a
node
this is with the slash it's not a query
string parameter
so for example um if i do site slash one
that's going to try to get me something
and it does not exist i don't have any
sites right now
if i do it as a query parameter that's
different so i'm not saying
id equals one
in that case it's going to
think that i didn't send anything so
it's going to try to add a site
because that's not a that's not a query
string parameter that's an actual
part of the url that we are addressing
here since if i just give it a query
string version then it thinks that it's
this because it gave me cite plus a
query string
so edit site if i give it an id it's
going to say it's checks to make sure
if it's not not zero so if it's not a
if it's an id that's not zero then it's
gonna go try to find the data for that
site
uh we saw what happens if it doesn't you
get a you know four one says hey i can't
find that
uh otherwise if it's a zero as an id
it's going to create a blank record
so which basically works identical to ad
but we just i did it this way so it's a
little more
uh ponderous approach if we post to this
site
so if i'm doing an update and posting
then i'm going to grab the data from my
form
and um i'm going to see if my form is
valid and then i'm going to go ahead and
save
and then i'm going to once i've saved my
record i'm going to go back to my list
of sites
and we'll take a look at this in action
a little later here
so i come through for edit i will create
a
blank form which i did here so it's from
site form and we'll take a look at those
in a minute
i'm going to send my id across and then
i'm going to have a title for the page
that i'm going to use
and then this is going to call
siteedit.html
for add site it's very similar i still
call site edit
basically the only difference is i just
come in and
create a blank form i don't ever
do the here where i'm trying to grab
an object from the database based on the
primary key
and so um two other things
i went ahead and i've created a logout
that i can call
and the user registration
user registration is so we can actually
create a user within our
application and that's a form that we'll
take a look at we've got a registration
form
and there's a registration html that's
just some additional
administrative pieces you really don't
need to log out the register
or even to check the authentication you
could just say i'm just going to let
whoever hits this application see
everything
so i built these
and one of the things i want to do now
is go for my views is we've
referenced these forms all over the
place forms
are the application version of
the model now i've got a couple forms
that i'm
i'm basically stealing from django which
is user creation form
in user and those are so i can do the
registration
and the user login so that gives me a
really easy way to
create and administer users those
pretty much you're just going to use
what you you know what comes you can see
there are
certain fields that you can require or
not you can make some tweaks to it but
that's sort of outside the scope of what
i want to cover this time around
i do want to look at my primary form is
the site form
and within that it is based off the site
model
sites model and i'm going to give it my
list of values that are going to be on
my form
for each of these i'm giving them with
labels i'm going to give them sort of
some pretty
labels for them and we'll see these in a
minute
and then there's some widgets that i've
got here to allow me to do
input most are pretty simple i've got
text where i'm limiting the size
and i'm giving it a class this is a css
class in case i wanted to play around
with that
um and then i've also got a text area so
text input
single line text area i have uh
multiple rows here i've got so i'm going
to do two rows of 80.
it probably should actually be three
rows because i have a 250 limit on that
one i think
and then um that's pretty much and then
a number input so
those are my forms and so now
i can go to uh let's look at our views
let's take a look at the the html side
of this
um there's two things one
first we're going to have a we're going
to add a static folder
which has to be addressed in settings
and that's where we're going to put uh
like our javascript our css
images things like that so those will
all go in our static folder
and then by default we get
a static url and then
and all we have to add is that our
um where our static files directory is
so i'm calling it static so it's just
off of my
off my base directory for my application
with static this os path join
is a operating system
generic approach to adding combining
values to give you a path for the
uh the hard drive for the you know the
local drive
so it takes care of things like you know
backslashes versus forward slashes and
things of that nature
and again this is stuff where you not a
whole lot you need to do other than just
know that it's out there
and once you've got it you know once
you've googled it you throw it in there
and boom
you know you're ready to go now within
my static
i pulled down the bootstrap files i
think we've seen this before so i'm not
going to
go dig that up and this just happens to
be because i'm going to use bootstrap
i do although i don't need to use this
blog guy
i'm not actually using him anywhere so
i'll just move him to trash
within the css uh one of the things that
bootstrap gave me is the form helpers
javascript monitor css is my application
and then i've got these images in case i
need them
now static comes in use when i go to my
templates
so templates is where it's going to look
for html stuff
so when i go to you i'm sorry a view
and i look and i say you know create
register.html then it's going to try to
look for that within
the templates folder and so i have
site edit which i've created here
and now with each of these so let's
start with our welcome
uh it extends base.html so this
this is built on top of base html which
i'm going to look at in a second
if you remember we have blocks
and so we have this content block that
is going to be
part of this html before the block
that content block it's going to go pull
in
base html base html is
our generic format for a page
we're going to load the static files so
everything that's within that static
folder
we load it and then a lot of this
is like here static we're going to pull
in our bootstrap stuff both the css and
the javascript
um we've got the form helpers one we've
got some jquery javascript that's pulled
in for bootstrap
pulls in another little javascript and
now we pull in at la one of the last
things we do is
and all of these are sort of your
standard uh link for a style sheet
h reference and then i'm gonna give it
uh within stat
uh within static css
monitor gives me whoops and i just
accidentally hit that
pulls in my my css i create a header
which i'm using
this is a pretty standard bootstrap
navigation in effect it gives us this
little thing here
which also is responsive so as i shrink
it down it gives me my little
hamburger menu so i do that to give me a
nice little responsive interface
uh let's see i get my i set up my menu
items
which is just basically a home and then
if you're logged in
uh i've actually i'm using some it's
still pi uh django so i can say if the
users log in if they're authenticated
then i'm going to allow them to have ad
site and login i'm sorry log out
if they're not authenticated then
they're going to be asked to log in so
they just get home and log
in so if i log out i just get home and
log in
if i log in i get home
add site and log out
and then i have my content block
and i'm wrapping that in a div that's
going to be of class content
i'm going to see that with our style
sheets a little bit and then i have at
the bottom
i have a footer which is going to be of
the standard footer class
and i've just got some basic text for
that at the very end
pull in some additional uh javascript
which i think didn't already have that
one bootstrap yes i did
i pulled in some javascript that i
actually pushed that stuff to the bottom
just for speed and so it comes in at the
end and then
finally comes in and pulls in javascript
that it needs
so that gives me our base so i've got a
footer i've got a header
and then i've got my content so if i
look at welcome
there's a header up here uh this content
block and then there's going to be a
footer underneath it
my content block i've just got i've got
javascript included here which is very
simple
for my on clicks so i can log in and
register
i've got a little text and so
i can see that right here uh oh i'm
sorry that's actually not it this is the
welcome
so i can see i've got my login button
i've got my
register with the site got login
registered the site register
i did not have one of my templates set
up so we're not going to look at that
right now
oh i'm sorry i can check i can fix that
real quick
it's not called external it's called
base
and i think that will give me all i need
so now
yeah so now i can do user registration
so i could add a user if i wanted to uh
it gives me some help for each of these
what is it
you know as far as your username your
password
and it's a very straightforward uh here
let's see so i do
if you guys remember uh i bring in the
registration
form and let's go look over here real
quick
for user register so i'm using this
registration
form which i pass in i pass the form
as form is the parameter
i'm going to pass that into register dot
html
and so i can come in on the register
side and i can do a form
and i can either there's three ways i
can do it i can do it as a list
as an unordered list as paragraphs or as
table
if i want to do a table i can change
that real quick
and it's going to be a series of rows
if i do that as a registration now it
looks more like a table
i get to the end after the table
if you remember i have to send it a csf
csrf
token because i'm going to be if i'm
going to do data entry stuff
and then i have my submit button
and then i've so i've put this together
and i've also got my little helpful text
you can also log in if you have an
account log
in bam bam and so i
i built my header which is the
navigation i've got my footer
which is down here which is also very
straightforward
and then this is my content now you you
may see that i've got my nice little
coloring for that that i picked up with
my
css uh monitor
if you remember i used a class content
for that main block and so with each of
those
i've just come in and i set a width
which is 90
i have it centered basically because
it's got a padding to the left
of 5 so it just kicks it over five
percent
i also take the text within it and push
that over five percent
and then i've got my background color my
forward color and then my height
is just eighty percent so it just sort
of limits it so it's a nice little small
screen very
uh you know early 90s look and it's also
responsive so doesn't look too bad as it
shifts stuff around
now uh that is so if i want to add a
site
so that's my welcome site list is what
i'm looking at here this one's a little
more complicated
um i've got an if here so if the user
has a first name then it's going to say
sites for that user otherwise it'll say
my sites
and then i've got my add a site button
which just takes me to add site which is
right here otherwise i'm going to say if
i have
let's see so if sites which is if you
remember that's a list of the records
from the sites table
if that has some entries then it's just
going to be a force of foresight
insights
so for each record i'm going to call it
site and then
i'm going to come in and build out a
listing of those which we'll see that in
just a second
and then i do an end for if i don't have
any then it's just going to say no what
sites entered which i have here
click on add a site to get started
and we'll take a look at this second i'm
i've got each of these displayed
there's going to be a css class site
which we're going to look at in just a
second so let's create a site
and this again oops let's look at that
real quick create a site i'm doing a
form as table
and i use that site form that we use
here
so i'm going to see site name
description test url ping type
blah blah blah i'm going to see all that
kind of stuff so i'm just going to it's
going to display
that out i set the table or the title
which is either add or edit
and then i've got a save changes and
then note
i've also got my csrf token so i don't
have to worry about
cross-site issues so i'm going to add a
site
i'm going to do it this is the dp
homepage
heartbeat
the ping type and i just had a couple so
i'm gonna do a heartbeat
uh the test url is https going back
slash
developpreneur.com
desired type is uh going to be is alive
desired value don't have to worry about
that because if is alive it's just
going to be a response uh and i'm going
to do this
every five minutes and so i'm gonna
that's my
heartbeat check when i do save changes
now i could see a list of sites
uh which is whoops site list
and so here this is where it's going to
for each record within there it's going
to do
an md5 and an md1
which we can there we go as it gets
smaller the 5 is actually
because of the size of the screen it
gets funky pretty quick but
normally what i'm going to see is if i
have multiple rows
let me add a second site let's see
i'll do my this one real quick
i'll do the same thing i'm going to do a
heartbeat
calm i'll go straight to that page
simple response well it's gonna be is
alive
don't have to worry about that and do
that once an hour
save changes so now we can see two sites
together
each of these i've cleaned these up a
little bit and
i can either edit or nope i didn't have
an on click so if i want to edit one
then i can come in and i can edit that
page
uh from this so let's take a look at
from the uh
the css point of view uh because that's
this is something we wanna we're gonna
wanna play around with a little bit i
think to make our applications more
useful
and because of the power that uh django
gives us
it makes it pretty easy for us to
you know clean this stuff up and well we
can use css anywhere
apologies um
so here i'm sorry so we're going to go
over our css and we want i wanted to
step into this a little bit just because
we haven't spent much time on it
so if we look at our what did i call
that i call
that site
so here's my class
and i can do quite a bit with this as
you can see this is a
mother moderately lengthy
css entry and i and with this i'm doing
things like i'm setting the radius we
can see i
as i hover i get my little background
it has a sort of beveled kind of look
and i found the easiest way for us to
play around with this stuff
is actually through pretty much any of
your modern browsers
if you want to change a look and feel
then you go up here so like
here i'm selecting it's hard to see i
know this is very small text but
i'm selecting this item so i'm going to
select
a site and then over on the side i'm
going to be able to actually see what is
the css that's being used
and i can start playing with this live
so i can change my background color
let's say
instead of that gray i want it to be uh
red yeah that's so that's pretty ugly
but
you know i could say instead of the
text color the one that it was
i want to change it to be oh i don't set
the color
uh but whoops
let's see if i can add a color real
quick uh shoot i can't because
okay there we go there we go so now so
if i do color
uh white then i can change that
um i can also change around my padding
so i can go from
25 to 30.
or i can drop it to nothing and see that
now my padding has been moved over
i can work with my margins
so i've got a bottom margin here i've
got a little bit of space
i could get rid of that i could start
cranking up the
uh and within the padding so now i can
like really tighten some of this kind of
stuff up
i could increase the font i
could decrease the font uh whoops
that's not decreasing the font that's
decreasing the font
and then the the quick way to play
around this stuff is play around with it
live
and then you can just copy and paste and
just take all of that copy it out of
there
flip over here and i could make that
part of my css and boom i'm off and
running
now that that hits i know we haven't
gone too deep into this but that does
give us at this
point uh an application
that we're entering some stuff i've got
some basic css we'll talk about this a
little bit more
uh in a future session we can enter some
data and that leaves us
the next time we come around actually
with this with this information to build
a back-end that actually is going to do
these checks based on pings and things
of that nature
so what have we covered we went through
the basic app
creation uh actually basic project
creation
and that included creating an
application with it we looked at forms
we looked at our model
views urls went through some nice basic
stuff
put our application together that way
looked at setting up our css and
actually including that in
and it's again much like any other web
application you're using the same kinds
of
commands it gets down to its javascript
and html
i talked a little bit about some
troubleshooting kinds of things as far
as you know being
having debug turned on and being able to
play around with that
and using our django environment for
things like
um backing up our data
and importing it into others um of
course doing our migrations and things
of that nature
so questions and comments
yeah i just uh i wanted to say it's all
more reason
i'd love to uh see if you see if i could
use this um
you know django to to monitor one of my
apps maybe not
write the code in in python but
um uh you know do some monitoring and
just uh basic operations what you've
shown here
uh in django just for how easy it is to
set up that's awesome
yeah it's definitely there's a as a
uh an ancillary application it is
actually a really
solid solution for that so when you
and i know you you've spent a lot of
times building you know like utilities
and things like that those applications
and
and uh little executables and that that
we have to build
or we don't have to but we usually end
up building
because we want to be more productive
because there's things that we find
ourselves doing all the time and we want
to automate the task or simplify it a
task or
do it in a way that's somebody that's
not us that doesn't have maybe the
the deep knowledge of the core
application that they can
do stuff that they can you know do some
administration or do some reviews or
reports or things like that
and uh the more i play around with the
more that i find that both
the python itself and because of it's
so lightweight and so uh
pervasive as far as environments and
things like that
uh and then django if you want to an
interface for it
it's a really good pairing for doing
those kinds of applications
so good point glad that you you're
seeing the usage as well
other questions and comments
if anybody's talking they're on mute
otherwise go ahead and move forward
so that brings this session uh to a wrap
i
had hoped to get further into it but
just as i started looking at it and some
of the things i wanted to cover
this is definitely going to have to be a
multi-part session
we've done we've got the bulk of it
we've got actually a very
we have a functional front end i think
this would count for like a
minimally viable product from the front
end point of view
uh we'll do a little bit of tweaks to it
and then we'll come back and swing
around to the back end uh next time
around
as always i want to appreciate your time
thank you for you know listening in for
checking this out for
comments and everything else if you have
any other questions or anything else
that you need to contact us
you can always catch us at email info
developer dot com
you can go out to the contact us form on
developernoor.com
we have a youtube channel that you can
go if you go
look for developpreneur from youtube
then you'll find it
and you can subscribe and we put stuff
out uh twice a week
tuesdays and thursdays right now we've
been a pretty steady schedule for the
last year so i'll probably maintain
vimeo is not um
not as frequent as what we've put out
from the youtube side
we more often will put things like the
these mentor classes will show up at
vimeo first
and then eventually they'll show up on
youtube
and then uh if you want to keep us from
the podcast point of view you can always
check out the better developers podcast
or develop an order podcast
it goes by both names for example if you
have alexa or something like that say
you know alexa enable better building
better developers
and it'll start playing the latest
episode and just
some of the ways we have to communicate
to you guys and
ways for you to communicate back to us
as we work together to try to make
every developer better so go out
have yourself a great day
[Music]
you