Deploying to Heroku Server | Django (3.0) Crash Course Tutorials (pt 23)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so in this video we're gonna be putting our jingle project up on a live server so we can access this website from anywhere in the world just by typing in the domain into the browser so for our server we're gonna use Heroku and we're gonna set up an account right now and then actually launch that django app on to Heroku so if you don't have a Heroku server or Heroku account go ahead and just Google up Heroku you can find them in the first link here set up an account and at this point I don't think you need a credit card I didn't set one up and it let me do this so go ahead and create an account if you don't have one so I'm gonna show you two different methods to launch Django site on to Heroku so the first method or the default method is usually to just use the Heroku CLI but I'm gonna try to make this even simpler and use the get method so this is basically putting our git or our website onto a github repository so a private github repository and then connecting it to a Roku app and this personally seems more native for me so I'm gonna start with that and just to get going here what I'm gonna do is once we're logged in is we're gonna create a new app within Heroku so we're gonna need to give our app a name and this needs to be unique so if anyone else with the heroic account has this it won't let you do this so I'm gonna use Dennis Ivey CRM one so this may take a few tries so this is available we'll just go ahead and create app so this is gonna give us a project within Heroku so if we go back to our main dashboard will see our app appear here so once we're here we can just go ahead and open the app so this is my app settings and we can we're gonna work within these tabs here but we can open up the app and this is gonna give us that default page here that says hey this app is live this is actually accessible from anywhere in the world if you just type in that in domain I'll probably take it down by the time this video is up but this is a working project so what we're gonna do is actually upload our jingle project and replace this so we'll be able to see our project that we built or if you're coming here from another video you're just trying to launch yours you'll be able to put your project up on here so what we're gonna do now is download first we're gonna download it so if you already have yet you do if you don't go ahead and just download it and then we need to download the Heroku CLI so this is just gonna give us the Heroku commands that we can work with within our command prompt on our local computer so go ahead and find this and then choose the download that you need so if your Mac use this one I use the windows 64-bit installer and this is gonna give us that installer and then once that's ready we can go ahead and just run through that process the Heroku CLI which I already have so I'm gonna stop this installation process but go ahead and set that up so once you have the Heroku CLI installed we can actually go to our command prompt and just do something like this we'll just do Heroku and then log in and this will just log us into whatever Heroku account that we're currently set up with so it's probably gonna open this up in my other window here so I'll drag this in I have two screens open but give it a minute go ahead and hit enter and what we're gonna see is this page ok perfect that's exactly what we wanted so we're gonna see this page pop up and if we just go ahead and hit login this will log in our command prompt into your Heroku so once we're logged in it tells us we can close this and we can start working with Heroku commands so the first thing we want to do is go ahead and tap into this project here so we want to find Dennis IV - CRM so in order to connect to that we're gonna do Heroku get and then remote - a and then our project name so mine was Dennis IV - CRM 1 and I spelled Heroku wrong so give me a second to fix that okay so we're now connected and just to confirm that it lets us know where that gate repository is we can just do get - V and actually it's get - remote and that'll let us know which project we're a in and let me just confirm that one more time so it's get - or get remote - V and there we go so it lets us know that we're tapped into this so if we have multiple projects that's how we can switch in between and if we want to create a new project directly from the command line we can do Heroku create and then give this a name so I'll just call this Dennis testing and what this is gonna do is actually create a new project for us up on Heroku so if I actually tells us it's created if i refresh this we're gonna see a new app in here so we can create it either just by doing new and create or directly from the command line so let's go ahead and make sure we're in the right project so get remote - wheat and we want to be within Dennis IV - CRM so that does it for the CLI for now so I'm going to minimize this but just make sure you're within the right app in your Heroku project so I'm gonna minimize all of this and open up my Django project and I did set up a virtual environment right within my Django app and I currently have it active and once I setup that fresh environment I just ran the pip installs that my project needs so if you're part of my Django crash course and you're trying to launch the same exact app these are the requirements so far so we have django Django filter Jane Doe storages for anyone that used the s3 buckets Bodo 3 for the s3 buckets and pillow and that's all we have for now so those are installed into my virtual environment and at this point I need to install two more things so we're gonna install G unicorn and I'll explain these in a second so G unicorn and we're gonna install white noise and I can just go ahead and just chain these but you could do them individually so we're gonna install these and once we have these pip installs what we need to do now is set up the required files that Heroku needs so Heroku has a minimal requirement for these files and one of those is a requirements txt file and what this does is basically let us know where let's Heroku know which requirements or which libraries do we need to work to make this project work so let me just show you what I mean by that so I'm gonna do pip freeze and then angle bracket require mints dot txt so what this is going to do is create this requirements txt file and it's gonna throw in the name of all these packages that our project requires so if we have a certain third-party package like Jango G unicorn white noise what it's gonna do and I'll just go ahead and run it it went ahead and created this file and if we run this again later on if it sees this file it's actually gonna go ahead and it's not gonna recreate it it's just gonna add in our new requirements and let me try to explain this one more time because I don't feel like my first explanation was right so when we upload to Heroku Heroku needs to know which dependencies this project needs so it's gonna look into the requirements txt file and go ahead and download everything in here to make our project work so if I pull up the Heroku documentation I'll just read you the description so when you deploy to Heroku the dependencies you specify in your requirements ixt file are automatically installed before you start up your app so basically Heroku is gonna go ahead look in here and set everything up for us and this is why we need this file so I hope that makes sense the next file we're gonna need to add is gonna be runtime dot txt and this is just telling Heroku I'm gonna save this dot txt and this is just telling Heroku which version of python we want it to use so I believe if we don't specify this it's gonna use the latest but honestly I don't really remember so I'm just gonna go ahead and use a method that I know is safest and we are using Python version 3.8 one so point eight one and that just tells Heroku it's gonna look into our runtime file and know to use Python three point eight one so now we're gonna add a file called proc file and I'll explain this in a second but just do proc file with no extension so the Heroku documentation clearly specifies do not add a dot txt extension or anything like that and I'll just bring in the docs to this which I will link up in the description so a proc file basically runs at the beginning so whenever our app is launched and we're gonna connect it to our WSGI server there so this is more of a Heroku thing I'm not gonna try to explain it but I'll just go ahead and write out what we're gonna set up into it and I recommend you read into that later so we're gonna specify a process type this is a Heroku thing and we're gonna do web and we're gonna use G unicorn we're gonna corn however you want to say that I actually don't know so G unicorn and we're gonna do CRM one so whatever we called our project or wherever your WSGI file is sitting so mine isn't CRM one WSGI so CRM one dot WSGI and we're gonna do - - log - file and we'll close it out with a - so that's our prog file make sure you have that probably configured and you have G unicorn installed and in your requirements txt and you ran that pip install that's very important because it is gonna go ahead and start up our app using this right here so in our settings WI file I'm gonna go ahead and just close these out we need to specify a few more things so I want this app to run on my local server and on my Heroku server so if I go here and find that domain so Dennis IV CRM one we're gonna open up that app and grab this domain to it so I'm just gonna get everything here and we're going to tell our project to allow this domain to point to our site and we're gonna remove that forward slash and this and I also want my app to work with my local server so we're just gonna go ahead and allow that too because if now now if I run my app on my local server it's gonna give me an air and say this is not an allowed host so we're just gonna paste that in and remove 8,000 and we'll remove that HTTP so we have that set and we are also going to set debug to false for now because we are putting this up on our server and on Heroku we need to go to our app and we need to specify build pack so within the setting tab we need to let Heroku know which language we're working within because it won't deploy without this so we just need to specify Python and save that and that'll run once we deploy our project so if we go back to settings for those of you part of the series I am I did revert back to you SQLite for now and we're not using s3 anymore if you happen to be using that so we're gonna have everything be served from our Django project so let's go ahead and open up github and actually create a git repository for our app here so let's go ahead and create a new one so we'll just do a create new app and in here we're just gonna call this CRM - live and we're gonna set that to private and initialize a readme file and go ahead and create that repository so I mentioned that there were two ways to deploy this so the first one is gonna be with github so the Heroku CLI github and I've never used this method but we're gonna use github so what we need to do is connect to our github repository so it's already connected to mine and we to find this repo name so if I do a search for CRM and we'll just do that and I have a few of these so there's the one that I have the source code with but we want to do CRM live so we're connecting to this empty github repository right here and we're not gonna deploy it just yet so to deploy it we just go ahead and hit deploy from the master branch but what I want to do now is actually upload our project to Gibbs so if we grab our project and let me close the stuff out if we get our project from here we're gonna go straight for the source code and I am gonna ignore that virtual environment for now actually we never want to upload that at least at this point what I'm gonna do is drag the files directly into the root so this is gonna take a minute but those files are going to transfer and what's their transfer to will actually push it live so let's just go ahead and give this a minute and normally you would want to use this you don't want to do this with the command prompt and you know push it that way so if you know github you should already understand that but I'm just dragging it in like this just to show you the process of it and not go through the details of how git works because that's not the point of this tutorial so we're just dragging the files in okay so our projects are now in our git repository so what we can do is actually go ahead and deploy it from here so we're connected to our git repository to that account here and we're going off of the master branch so and get help we have branches and we're going off the master which we can change later and we're just gonna go ahead and deploy that so I'm purposely leaving a few things out right now because we should get a couple of airs that I want to debug along with you so let's go ahead and watch this go through and it's gonna start the build process of actually deploying our app so if I go ahead and open the app up I'll open it up in another tab here we're gonna see it appear here soon so let's go ahead and watch this okay so we got the first air and what's happening here is Jango is not all right Heroku doesn't serve static files for us so what it's trying to do is run this collect static command and if you're not familiar with that I'll probably cover that in another video but collect static just basically bunches up our static files and has a different way to serve them and what Heroku is trying to do is on push during that push it's trying to run collect static but we never sent a static route so it's trying to run that but it's given us an error so it's suggesting for now that we turned them off well we don't want to do that so what I'm gonna do because I do want our static files served is we want to configure the static route so just above our media URL we have our media route but now we need to set our static route so let's go ahead and just type that out so that's static underscore route and I'm gonna copy from here so OS path dot join baster and we're just gonna call this static files and I'll explain in a second what's going on here so static files so what's happening is any time we run Python managed py collects static all of our static files are gonna go in to that static files folder so this folder will actually get created and all of our static files will be thrown in there and if we're using s3 buckets what happens is when we run collect static all of our static files will be sent up to AWS s3 so that's what's trying to that's what Heroku is trying to do for us right now so when we set this static route Hiroko is gonna go ahead and run collect static and it needs to know where to put this so that's gonna fix this air for us so it's no longer gonna tell us to do this and we're gonna we're also gonna use white noise because heroku still doesn't know how to serve static files so white noise is gonna help us serve these so that's why we ran pip install and within our middleware we need to throw in this right here so white noise middleware and we're gonna throw that into settings right where was that middleware list I'll just put that up here so traditionally I've actually seen people put it underneath the security I I don't know if I've noticed a difference there so now that I have this let's go ahead and reach these files merge them and then actually fix this err for ourselves so I'm gonna go up to github and I'm gonna create a new repository and we'll just say white noise and for anybody that was I had a couple of questions that people asking how do you update a project after you push it well this is how we would do it if we were using github so we create a new branch and we're gonna go to that branch and as long as we're within that branch we just created we're gonna re-upload the files again you'd normally do this from your command prompt but we just made the edits in our settings @py file so we want those to be reflected so what I'm gonna do is grab those same files and we're gonna remove that env file because that's gonna be too much for us to try again we'll give this a few seconds once actually this will take a couple of minutes but once this gets throwing up on github in a new branch we're gonna go ahead and reemerge it with our master branch and then upload it and then that air should be fixed okay so now that that branch is up there let's go ahead and merge this new branch with that master branch because that's where we're serving our files from so white noise will now be merged with master and those changes that should reflect so it showed where we made those changes let's go ahead and create the pull request and again I'm showing this just because if you already understand github they should all make sense to you I'm just trying to show anybody new on how to redeploy to Heroku so once we can of those those changes should be reflected in our master branch so we can go ahead and just hit deploy branch now one thing about this if we select if we select automatic deploys this should configure it to where any time we make a new push to master we don't have to click deploy here it should automatically deploy itself so let's go ahead and see if that collects static issue goes away so let's give it a second so it looks like collects static is running correctly now okay so the branch deployed and we can view our app on that URL now and here we go so our website is now on Heroku and accessible from anywhere just by typing in that domain so let's go ahead and see if those static files work so we'll log in to our main profile all of our static file of our static assets look good and if I do four slash admin the admin panel also looks good so perfect that's exactly what we want it so the next method if you're not familiar with git and everything I just did basically we're gonna go back into the steps right up until deploying it with github and we are now gonna use the Heroku CLI so I'll actually create another project and we'll just reconfigure it look really quick because our files in our Django project are already set up so we'll just show you how to use the Heroku CLI to deploy this before we launch with our CLI I wanted to show you this air that everyone seems to always get and you're most likely gonna have this at some point in your launch process so basically what this is is you try to open up your application and something didn't work and Heroku throws this hair so it's not a standard 404 error it's gonna tell you something else so what's happening here is something went wrong during deployment so what you can do is actually within your local CLI so let's go ahead and I'm actually logged in so we'll just do will actually do exactly what it says here so we'll just write in Heroku logs - - tail so Heroku logs - - tail and I actually don't have an error at this point that's tail tails I don't have an error at this point but I'm just gonna show you what happens so whenever you type this in it's gonna show you the log files to what was going on when your app is being launched so you're gonna be able to read through this right here and see where that error occurred so it's a lot to read over at first but it's a good way to figure out what went wrong I don't have an example for you right now because we didn't run into anything but remember to run that command from your CLI so let's go ahead and close this out so I just pulled up an image from Google let's go ahead and close it out and actually start working with our project so I'll close this out with control C and terminate that and let's go ahead and clear this test and see LS there okay so the first thing you want to do is make sure you're logged in so Heroku login I'm logged in but I just want to make sure you remember to do this because this will throw in hair go ahead and hit enter and wait for that pop-up to occur and we can login by clicking that and again this will tell us our window can now be closed so let's go ahead and create a new project so we'll do Heroku create and we'll just do CRM live - CLI because we're using the Heroku CLI let's see if that's a real name that we can use ok so that worked and let's go ahead and tap into that name so we'll just do Heroku or that git repository git and we'll just do remote - a and then the app name which was CRM live - CLI and that should connect us so just to confirm that we'll do get - remote - B okay so we're in there and if you look at our github repository I actually closed out those other two projects or the other project that we had and we have the one we just launched with git and we have the new ones so if we open up the app it's currently just a blank app at this point so let's go ahead and start configuring things so we'll open up our command prompt and the first thing I'll do is get in it so we initialize it and we'll do get status so I'm working with in my virtual environment so I want to make sure everything's set up right so let's go ahead and actually ignore this E&V so remember when I dragged it into our Ghaleb account I didn't drag these files in so that's a lot to drag in so what we're gonna do and later on we're gonna do this to ignore or SQLite database we're gonna create a new file and it's can be called dot get ignore and in here we just want to ignore that env file so we'll just do env the forward slash and that should now ignore it on push so let's go back in here and right there we see that env file so let's just do get status and now we don't see it so it won't be pushing let's actually go ahead and ignore that too so we'll just do underscore underscore and there's a good documentation on what to ignore but I won't cover that right now so we'll just make sure it will just ignore what we need at this point underscore forward slash and then we can do get status and that should be gone now and now let's go ahead and just do git add and then we'll do get commit and this will just be our first come in so - em and we're gonna put a note and we'll just say our first commit so the reason I'm able to do this is because I'm actually in the project that I want to push to my new Heroku app so I'm in CRM one so I'm running all these commands from there so when we created and we start pushing it's gonna take everything from this this local project so we did get commit and I'll just check status again and everything looks ready so now we can push to our master which is that repository that we just created it's get push Heroku and master and that should just grab all of these files and push that into our new project so let's just give it a second and remember that we already configured our proc file requirements txt and run time so all those are being pushed so we shouldn't really get any airs at this point and this is actually a perfect teaching opportunity here because I realized we never changed our allowed hosts so we're still pointing to that other project so we had the two and I want to point to the new one and I also want to set up the build packs actually I realized that build packs were set up already so we want to point to this domain so when we open it up right now we're actually gonna get an air and what this is allowing me to teach you is how to repo something after change so once I change we change allowed hosts here let's go ahead and just update that remove all these so once we change this we need to go back into our command line and it will just do get status we'll see what's going on so it says that we modified our settings now py file so we'll just do get add and then dot and just to check that we'll just do get status again and that tells us okay we modified it now we're ready for a commit so we'll do git commit dash M and our comment will be updated or we'll just do a loud host so that's just a comment for ourselves and get push master so we made an update and we can push it it's Heroku I always forget these commands get push Heroku master okay that should do it okay so that just launched my app live again and that should fix everything so let's go ahead and just wait for this to refresh okay so that took care of everything and at this point we still have our SQLite database so let me just make sure everything's working our static files are working okay so in the next video what we're gonna do is actually ignore our SQLite database and connect to a Heroku database so within settings here in our configuration variables by default we already get Postgres database to work with so Heroku already sets us up with it it's not something you want to use in production but it is something we can use for now so what I'm gonna do is show you how to connect to that and ignore the SQLite database and then we did a video a couple videos back on how to put a database up on AWS so we're actually gonna connect to that database that I created so go ahead and stick around for that and we'll make this more production level ready and one more note if right now if we're loading up these sites they're kind of slow at this point what we can do is actually turn I'm not gonna make this a whole Iroko tutorial but we can actually crank these up by paying for more dinos here so so at this point our Dino type is free so once we start upgrading seven dollars a month if I just upgrade to that it's really gonna start enhancing our site and our site will speed up I won't go into that I'll probably just take care of that really quick in the next video but that's it for now
Info
Channel: Dennis Ivy
Views: 102,010
Rating: 4.8826289 out of 5
Keywords: Programming, Software Developer, Dennis Ivy, Dennis Ivanov, Django, Python, Heroku, Deployment, Server, Live
Id: kBwhtEIXGII
Channel Id: undefined
Length: 27min 38sec (1658 seconds)
Published: Fri Jan 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.