Go Live with Django and Heroku for Free | Python-Django Deployment Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this one we're going to be going live with Django and karoku Django of course is a web framework written in Python and Heroku is a web hosting service that uses git and makes it a lot easier to deploy make changes deploy again and do all sorts of things we actually run coding French burns comm on Heroku this is not a paid endorsement by any means if you are Heroku and you're watching this please we would love a sponsorship from you guys so we can make more videos like this but more specifically this is made to be useful for you I want to show you guys how to get your project going and live so it's out there it's out in the wild and it's actually working so to do this we're going to be using this guide right here so go to that URL check it out this is going to be updated as we get feedback from you guys so you must be at least a logged in user to comment but make comments there if there are changes or things you see wrong with this now first and foremost you're going to want to do the first three setup items to ensure that you're at where we are now first off is a Heroku ready blank Django project what that really means is a settings configuration where it's in its own folder as in its own module and you have three different setting modules along with this init file here in this case we have it commented out but basically you want to make sure that you have a base file that has this stuff and a production file that has this stuff now that stuff is the SSL and TLS stuff where you set it up in this guide now the reason this and it is basically if I uncomment this out I now have the ability to use my production file as default and then depending on what system you're on you're going to use that local file so if I go live I'm not going to have that local file that's something we'll actually show you to do that's actually not on this guy but we will show you how to do it here finally of course you want to make sure that you have get set up using into the guide or you can watch it which goes in junction with this guide so now let's actually get started first of all in our project we're going to make sure our git repo exists now I'm going to go ahead and close out a lot of these things and just go in here and go in to my project so I'm going to go into dev and I'm going to CD in to try Django 1-11 and I'm going to activate my virtual environment and then I'm going to go into my source file here and I'm going to run Python manager py run server I actually want to make sure that there are no issues going on here so I actually do have some issues and that's because I'm coming from a place where I only had everything on base pi but I made some changes to base pi within the project itself so as you see here I've installed apps on Sutton side a local I want to make sure that I have those installed apps as well as well as on production so this is something you're definitely going to want to do so if you follow just with that blank project you're going to want to make sure that you have everything set up that's associated to that given project so that install that now when I do that my errors go away that is definitely something you want to make sure is important there are a lot of errors that might come up when you bring this on to Heroku but for us we are now ready to actually go on and make sure git repository exists now if I hit ctrl C and do get status I actually see that it says modified well in this case this is actually working off of a get different github repository this is working out that live code so we actually send this code on our github so we actually don't want to send this code in the same way that is we want to make sure that our github repository is in the root of our Django project that is where manage type is so if I did LS that dot - al I see git is not in here by CD back and do LS that al then I actually see git is right there as well as the get ignore so I'm going to change back into my source project here and I'm going to go off of as if I didn't have a git repository so it's going to look something similar to this so all I do now is do get in it and now if I do get status it gives me all of these things right it's very similar to what's going on here mainly we've got our Django configuration folder here or where settings up high is as well as our managed up high folder which can be seen here movie picky is where settings is as well as WSGI now this could also be seen on our code for this particular project trying a 1.11 we go in and we look at that code that's all there - might be updated a little bit - what's actually on the video but the main thing here is the code is right and now I look and I see all of this stuff is in here okay cool so back into the guide I have everything pretty much ready to go we're going to login install and login to Heroku now you're going to want to download and install the command line interface which if you actually follow along with this guy you have already done and you're going to want to make sure you log in now if I actually type out Heroku I should see something like that right that is absolutely what you should see if you see command not found that means you didn't install it correctly and you're going to want to go back do that so now I'm going to create a Heroku project right so that is what I need to do I have get ready get status rented right so it gets ready now I'm ready to add a Heroku project so I'm going to do Heroku create and I'm going to call it mu E - picky because that's the name of my Django project notice these URLs here and notice that there's a repo in here so if I did get remote I see that I have Heroku in there so that's showing me that Heroku hat was installed - might get a repository or my git repository which is having not quite yet but it has all of these so my Heroku projects ready now what I need to do is create a git ignore and a proc file now get ignore is related to get it's not related to Heroku I mean it's important for Heroku because Heroku uses git but it doesn't actually do anything for Heroku it's all github related so what I'm going to do is I'm just going to copy this command here and just put dot py c c o py brackets Codd into get ignore so if I go into my actual project file itself I'll see that I have a get ignore here as well as in my virtual environment of course I'm going off of the one that just has dot pyc now we want to have a better get ignore file so there's actually a link here that takes you to a better get ignore which is the master get ignore for the Tri general project so this is actually going off of what's in the Tri Journal project in other why in other words it's going off of this right here but I'm going to go ahead and add it in and I save that and I'll do get status my status is roughly the same but now d/s store is gone so if I scroll up a little bit ICDs stores there and I no longer see it here that is good so that means it's actually being ignored that means when I do get push or get commit which we will do in a second I actually don't have that file KO'ing they're going on to the server which is important because I actually don't want this local file file going either which is shown up and in it right so I have this init file the reason this exists is because we only want production working so that means that I'm going to go back into my get ignore get the name of my Jango configuration folder wherever local de pie is or more specifically wherever WSGI del PI is or where your settings were originally you're going to grab that file go into the get ignore so it's that folder settings dot and then local PI you could also probably just put local dot PI or possibly even something like this not sure if that will work actually but this is going to go this is the path right to where my local settings are if you had a different project it'd be something like DJ project or my project or whatever CFE home it really depends on what it is in this case it's movie picky that's the name of this project itself so we're going to go ahead and save that that actually handles all of the get ignore stuff that we really need to worry about at this time so the next thing that I want to do is actually echo into my proc file so I'm gonna explain this proc file in just a second so I go echo web : G unicorn DJ project so this is not the correct name right so that's DJ project we have movie picky WSGI is in movie picky right so settings all of that stuff so we actually are going to go back and change this to being movie picky as one word hit enter and what you'll see in your project is a proc file here now this is definitely related to Roku so what it's saying is use G unicorn and run our Django project that's listed here right so that Django project the WSGI actually tells the environment where that jingle configurations folder is right so this is what it is alright so movie picky settings Oh what do you know that looks a lot like this get ignore except it has a different separator it's a slash over there and a period here but this is showing that default Django project so far so good we do get status again we now see that proc files on there you definitely want to see that you actually don't have to have your sequel Lite three database added so you can also add that in to your get ignore if you'd like and you can save that and you get status and your sequel Lite three database is gone okay so that leads us to provision our database right so I'm going to go ahead and provision a database and this is really a simple command but this is a free database so we can just go ahead and add it on so it's a free hobby dev that means we're just practicing things out database is empty that's what we want so good we now have Heroku x' database ready so that means we need to get our production settings ready for this database so we come back into our settings for production and we scroll down underneath databases here this is where I'm going to add in this stuff and I'm going to paste it underneath here and this adds in my database right so this adds in database related items so you don't actually have to this connection age but that just opens the database a little bit longer but really these three lines are the important parts of it and notice that it says databases and then as a bracket so you do want to keep this in here great it's going to replace the things that are in there but you want to keep at least the Django like youyou could just put it in like this you don't actually have to have sequel Lite listed at all so we'll just leave it like that and then another another couple things that you'll see in here is debug being false and allowed hosts so if I scroll to the top of this I actually already have debug being false but I don't have any allowed hosts so going back to my terminal if I scroll up into when I created the Heroku project I see this URL here this is actually the URL where our apps going to live by default we don't have our custom domain yet so we're going to just add that one in now if you did have a custom domain you would put it here so dot Mui picky com so that's our custom domain so I'm gonna go ahead and save that and now I have my production settings pretty much ready to go specifically for Heroku let's just double check everything number one debug is false number two we have our installed our allowed hosts on there we want to make sure that they're there and if we had a custom domain including subdomains this is what it's going to look like without subdomains you just get rid of it or you write out the actual subdomain itself so and then I scroll down a little bit I want to make sure that my databases are set up they are that's looking good scroll down even further I want to make sure that I have all of these SSL and HTTPS related things again this has everything to do with Heroku app our own custom domain name might not have SSL so if it doesn't then you're going to want to get rid of those settings so we scroll down a little bit now we've seen use of Heroku environment variables for Django keys now in here this is a secret key right we don't actually want this everywhere now of course ours is a practice one we're not actually using this this is not a real service or a real environment key so we can share this but yours you're going to want to keep it nice and secret that is if I'm going to put this on github as well as have it as a production project I'm going to want to make sure that this is hidden I'm going to do that by adding it in as an environment variable to Heroku so we're just going to go ahead and copy this real quick and we're going to do Heroku config set secret key this is our own custom environment variable that we put and I'm just going to go ahead and copy this paste it in but I'm going to change a couple things here and hit enter up looks like it won't allow it as that so let's put it in as a string so put a double quotes on both sides of it so quote 1 and that's probably because it's just how the command line is the code should be reflected accordingly all right so it's now setting this secret key and it restarted movie picky I mean we haven't sent anything and we can also do that with our email passwords we don't actually have any email passwords set up on the production so I'm going to just go ahead and grab it from my base and here's some of that email setup check out our blogs to learn more about email setup just do a quick search for email and you'll see different ways of setting things up so now that we've got this email setup we've also might want to change our password so again we're going to do Heroku config and then set and I'll just say email password equalling to your password right so you would set your email password here oops we want to put them right next to each other so it actually has to have no spaces there and now we set it we've got that there and then again in production now what we do is we're going to grab those environment variables we're using something called OS but environ environ get an email password so this is going off of the environment of the environment variables as we see here the same thing is true with the secret key so the secret key we're actually going to leave a default so OS environ get and we'll put secret key in here again and then we will leave our old one as the default remember I changed it so it is actually the default here so environment we set environment variables we gave them our own in variable names right so we that is our own variable name right there we could call it something different if we'd like in fact we did call it different on this guide itself but not going to worry about that you can play around with that as much as you want so now I'm going to go ahead and scale up my Dino so I'm just going to do Heroku PS scale web 1 it says it cannot find that process type so that's a little bit out of order so we'll come back to that but we want to make sure our requirements are correct that is I want to make sure that I have these things installed Django then PostgreSQL because that's the database I'm using the database URL and J unicorn are to have them installed so that's fine and so what I'm going to do then is add my requirements here and I'll even commit my requirements and do commit updated requirements there we go and now we are ready to actually push but before actually push finally to Roku I'm will disable collect static so I'm disabling collect static so by default Heroku will run Python managed to py collect static and I'm actually getting rid of it because I want you to use s3 for static files Heroku says it - we have a guide right there on how you can set it up but let's go ahead and do our commit now I'm going to do git add - - all loops let's close this out get add - all get commit initial commit notice it commits everything there and then we'll do git push Heroku master this is how you're going to actually always run your commit so you do get add and then add whatever you're going to want to commit and then write a message and then push it to Heroku Heroku is going to push it here's a few things that are to note Python app detected installing Python three point six point one it does that by default now as far as I know it installs pip and then it goes through and installs all of the requirements that we put in our requirements of txt file so we see Jane goes there pillow some other things are in there by default and then it discovering process types proc file declares types web so this is all coming from the proc file that's where that is that's how it was able to figure this out of course they have some other like smart things in there and it's actually working off of that as well alright so now we've got that we've disabled it now I can run my migrations so I'm going to go ahead and do Heroku run Python managed to py migrate so this is going to create my database it's going to migrate my database with my Django project of course if you make any changes to Django what you're going to want to do is save those changes and then run them inside of Django notice I have this key error default that is actually good that that's showed up I wanted to show you that hey maybe we actually should just keep this in here because it's looking for that key I took it off so I made some changes I'll do get add or well it's first off to get status get add and then I'll add this production file here to get commit update production database and then get push Heroku master and since it's going to be pushing it shouldn't go as long this time because it wasn't that initial push it's going to go less time so it's not going to install everything all over again it will detect things but it will go off of any cache that it's already had see it didn't actually do it and if you had collects Fedak on which I don't recommend but if you did have it on it would actually collect aesthetic files also during this process if you did have it on and you didn't have your static file set up correctly it would run some errors okay so now that we've done that let's go ahead and run Heroku run Python minutes up py migrated again this time it should actually run our database because there should be no errors now and I'm going to also mention that hares are some other common sort of things that you might end up doing notice I ran all of those migrations hopefully it actually went off of the database because it doesn't look like it did it looks like it just went off of something different so what I'm going to do is I'm going to come back up and I want to make sure that I actually have my database in there so I'm going to do Heroku config and we have our database URL it is in there so I want to make sure also that my production file is correct I'm going to comment out this comment out that get status get ad - - all get commit testing production settings module get push Heroku master so again this is just making sure that only my production file is working so that's the one that it's going to be coming off of because my database says that it was already migrated it said it already ran those migrations like it didn't create the migrations from scratch and this could be because we deleted our database dot sequel Lite that might be why or I should say not delete it but it's not actually on our server so let's go ahead and do Heroku run Python minutes up py migrate so what we want to see is it's that it's creating these tables and it's creating them on our Django project itself and that's what we actually wanted to see right there so for some reason it wasn't actually doing that so bringing back in and might because local dot pi was actually included in our get ignore so I'm going to go ahead and bring it back in but this time change the name to it to being local to rename this as local - close that out and then we'll do git status local - is on here right now it says it's deleted that so it actually was and get to local was there that's that's why it didn't actually run it so back into our get ignore here we have some issues with our get ignored here there's our issues it wasn't actually ignoring all the files we wanted it to so we do get status again local is still there but I'm going to bring in local to now get status now it's back in it is going to be fine notice that it's saying deleted local but local two went away so I'll do get get add that's just all get commits updated a get ignore and get push Heroku master and now we should say with fairly certain confidence that it's not using sequel light database it's going to be using the Heroku database and then I'm also going to run some commands to ensure this because our local database actually has some items in it while that's right I'll go ahead and open it up to prove it to you guys so CD try Django 111 source bin flush activate CD source Python Mandel py Ron and on that server we're going to go into it oops let's go to the local server here template does not exist so we have a template error related to our directories so we don't actually have the templates set up there we have it in base so that's going to have to be updated okay and also in production we're going to do it there too because there will be errors good save it now we refresh this is local see that it has some stuff in here and even if I wasn't logged in I probably wouldn't be able to see that but I also won't be able to log in because I haven't created a super user just yet so I made those changes I do get status get add dash dash all get commit update action templates location get push Heroku master and it's going to run that Heroku push I'll let it do that for a moment okay it's pushed so I'll do Heroku open to see this project it's loading and hopefully we're not going to see any errors here there it is there are no errors let's go ahead and try to log in and it's likely that this doesn't work so I don't actually remember that users password locally so I'll just do Heroku run bash to show you how that works so Heroku Bash is the shell kind of like the linux shell on Heroku so this bash is actually going to work like Linux so if you're on Windows you're going to have to use that convention like LS so we're going to do Python manage that py and we'll create super user and I'll give the username J Mitchell 3 here's also another example as to it's not local because that is not my local username to contrast that Python managed to py create super user it gives me a different sort of user name set up right so Jay Mitchell 3 we're going to leave the email address this is where you're going to want to have a good password ok so key user 2 we have probably a little bit of issues we definitely have some issues on this code in particular so let's go ahead and fix those issues sorry for those of you who are following along and not using our Django project that we worked on or try doing a 1.11 but we want to actually update this so I'm going to exit that out I think the admin should still work for this user so let's go ahead and try it because it's a post save signal and it did okay cool so it's actually still working there's just a little bit of code error as far as the posts a signal receiver for this particular project but I can now login I can add items so in this case I'm just going to add some items that we were using as a as an example to show you that yeah the database is actually saving these items I go into the admin and I should only see those items in there so inside of items click on that oh wait actually we did restaurants so inside of rational locations we see that there's a restaurant location here now you might be wondering why the Styles aren't there or why Django admin looks different and that's because I didn't actually set up the s3 files as recommended right here okay so now the last thing I wanted to show you is actually jumping into Heroku and show you where the configuration file or where the configuration is so you're going to jump into your app in my case it's movie picky we're going to go into settings here reveal configuration variables and here we are so I can actually change these things so let's change this to something different I'll go in here and edit it out it saved changes now I can actually edit my password and say new password or whatever I'll hit save changes and that is now setup as my Heroku project or Heroku app all I have to do is restarted so we do Heroku restart and that will actually restart my dinos and I also add those changes into my project removed issues git push Heroku master and one thing I will also mention since we are now in production and we have debug being false by default Jango will send your emails or send you emails to your admin so the are the errors are the issues that happen so when we saw that issue happen when we're trying to save that user or say create that super user for scroll up when we saw this that will actually be emailed to you too even though we did it inside of Heroku okay so I know it's a lot and I realized that there were some issues and errors when coming through do you realize this happens and I do really really encourage you guys so everyone can gain either comment here on our guide itself or comment on the video below so that other people can actually get some information from this so we can all launch our projects better and also make sure that this guide is as best as it possibly can be because I've noticed some issues with the Heroku guy itself one of the big ones being and hopefully they'll change that by the time this is out but one of the big ones being that it still says sinc DB and that is a very old version of Django instead of using my great thanks so much for watching and thank you for going through this whole process I know is long but once you get this down you can actually have your projects out there and live much like I do so if you want to stick around I'm actually going to show you how to put a custom domain on here as well as SSL so to do this we first off have to understand that this is going to be paid you have to be a paid user to actually have your automated certificate that's that automated SSL and it's using something called let's encrypt so to do this I'm going to just upgrade my service from the hobby or the dev hobby to just hobby let me go ahead and resize it so this is going to do that if I can't actually resize it then I will have to you know upgrade locally or on Heroku itself so I'm just going to go ahead and do that and say configure dinos here and we're going to upgrade this from our hobby or from free to hobby all its saved once it's doing this seven dollars a month saying then I can actually use their automated certificate stuff so to add our domain name will just do Heroku domains ad and in my case it's W movie picking calm and it's adding that domain name so it's going to take just a moment and while it's doing that I'm going to log into my account so now my account for movie Peggy comm and my DNS records and it's also added to Heroku is DNS and all we have to do is point the DNS target to this so I'm gonna go ahead and copy that put my answer here WW type cname hit add record I'm add some URL forwarding I'm going to use an empty domain name and do WWWE picky comm that means if I don't have a subdomain it's going to just refor dit to movie picky so I've got my DNS records there all that stuff's looking good I can go ahead and run Heroku domains movie picki done no problem go back into our certificate portion and then we're just going to go ahead into Heroku certs auto enable so of course you have to have a custom domain to make sure that this is working and we can check our status on these certificates by doing Auto notice it has my domain name here it's in progress right now so I'm gonna let it wait for a second and some domains are failing please verify your DNS matches the Heroku domains it's possible that this is not saved correctly so let's go ahead and make sure that we check that again so the Heroku domains it's possible that also the SSL is different and yep sure enough it is so it should be pointed at this so let's go back in to our domain names will edit here and it's WWE and instead of that we'll do dot Heroku SSL will it update let's see if it's working now it might take a second for it to actually be correct now it's no longer failing so it's back into into progress and we'll come back in just a moment to check it all right so it's still in progress but I actually changed it back to row DNS like it said initially I get going back and forth on on checking these things out and I'm just going to try to go to muy picky calm and it should redirect me it does redirect meaning it's saying it can't provide a secure connection so it's the certificates still not there yet but do you note that muy piggy comm is on my production PI otherwise it will definitely not work so going back checking those certificates still saying that it's failing so it might take some time for this to actually propagate so the DNS servers to propagate correctly otherwise you'll constantly have these problems and again Heroku domains if we run Heroku domains will see the Heroku domain is this right here and that's what we have so movie piggy is going here and let's try it again sorts auto still in progress so it's going back and forth and checking it looks like it's not fully there yet but it should be very very soon so we'll come back and check it and I also just decided I was going to go ahead and test the other sub domain so it got rid of that URL 14 and I just put the root domain there without a sub domain also added it so basically I did Heroku domains ad just without the WWE and then I added sorts Auto and then it's back to testing and not testing after reading further it says it takes 45 to 60 minutes so we'll actually come back to ensure that this is working correctly after that time has elapsed hopefully it will be it should be because I've done this many many times that it now absolutely does work this way so if it doesn't then we would basically just have to contact Heroku and make sure that their side of things is working because this is definitely the process that they said and we're really going off of their automated certificate management stuff as it fits so that's a little bit of a bonus for you guys and again we'll come back and take a look okay so now it's only a few minutes later actually it didn't take 45 to 60 minutes but I think they say that because people like me get antsy to make sure it's done it's now saying DNS verified so what that means is if I click enter here it actually should give me my site it is not currently so there we go it is now working so Mui Peggy calm is on HTTP so this is actually what we end up building right but what you're going to actually see in the walkthrough in probably you've already seen if you've watched Django a 1.11 is a more styled version of it which all of that code of course is available on our github and that is github PACOM or just join CFE comm slash github that will get you there much much faster so hopefully this guide was really useful for you and thanks so much for watching the whole thing or the parts that you really needed I hope to see you guys again in the near future and don't forget to subscribe join CFE com /youtube that's join CFE com /youtube that is where you're going to want to subscribe that will take you right into our Channel and you're going to want to go ahead and click Subscribe and that little bell so you make sure that you get all of our new stuff see you soon
Info
Channel: CodingEntrepreneurs
Views: 39,159
Rating: undefined out of 5
Keywords: djangourlshortcfe2017, django url shortener, django 1.11 tutorial, django tutorial, install django on mac, install django with pip, install django with virtualenv, virtualenv, python django, Django Web Framework (Software), Mac OS (Operating System), Python (Software), web application development, learn django, installing django on mac, pip, python package, django, kickstarter funded, beginners tutorial, trydjango2016, heroku, django 1.11, launch heroku, muypicky.com, heroku.com
Id: 4DggiEkbCTg
Channel Id: undefined
Length: 37min 2sec (2222 seconds)
Published: Mon Jul 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.