# Python Django Tutorial: Deploying Your Application (Option #2) - Deploy using Heroku

## Метаданные

- **Канал:** Corey Schafer
- **YouTube:** https://www.youtube.com/watch?v=6DI_7Zja8Zc
- **Дата:** 19.02.2019
- **Длительность:** 53:30
- **Просмотры:** 294,051
- **Источник:** https://ekstraktznaniy.ru/video/11981

## Описание

In this Python Django Tutorial, we will be learning how to deploy our application to Heroku. Heroku is a platform that abstracts away a lot of the low-level system administration and allows us to easily deploy, update, and rollback changes for our application. Let's get started...

Git Install - https://git-scm.com/downloads
Heroku CLI Install - http://bit.ly/heroku-cli
Postgres Install - http://bit.ly/heroku-postgres

My requirements.txt file - http://bit.ly/django-req-txt-file

Git Command-Line Fundamentals - https://youtu.be/HVsySz-h9r4
Gitignore file (Python) - http://bit.ly/py-ignore

Environment Variables (Windows) - https://youtu.be/IolxqkL7cD8
Environment Variables (Mac and Linux) - https://youtu.be/5iWhQWVXosU


✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms

✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join

✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY

✅ Cryptocurrency Donations:
Bitcoin Wallet - 

## Транскрипт

### Introduction []

hey there how's it going everybody in this video we're gonna learn how to deploy our django application using Heroku so that anyone with internet access can use it within a browser so far we've created this application with some good functionality but the site still only lives on our local machine so right now I have this running in my browser on my local machine but no one else can access this so the point of building these sites and applications is to deploy them so that they're accessible over the Internet so in this video we're going to use Heroku to deploy our application now if you don't know what Heroku is it's a platform that allows us to easily deploy and host applications without going through the headache of setting up everything on our own manually so if you watch my video on how to deploy to a Linux server then you would have noticed that took a lot of time and also requires you to be familiar with a little bit of system administration and things like that in order to get your application up and running Heroku abstracts a lot of that away and allows us to get our application deployed more easily without needing to set up our own web servers and firewalls and stuff like that and they also have a free tier that you can use so that you can actually get your application deployed without paying any money and then if you need to add any features that aren't being included for free then you can simply scale up however you need now one thing that I should mention about Heroku is that they don't have a normal file system like we're used to it will hold our static files and things like that but if we store a bunch of images or user uploads then those are actually going to be deleted so if you're saving images or files from your users then you'll need to use a service through something like Amazon Web Services in s3 buckets so if you're following along with this series then we already saw how to get that set up in our last video okay so with that said let's go ahead and get started so first you're going to need to create a Heroku account this is pretty straightforward you're just going to want to go to their website and create an account just like you would on any other site so I've already gone and signed up for an account through them but if you hasn't haven't then you can just go to sign up and fill in all your information and just register like you would with any other site now once you've created an count we also have a few prerequisites that we need to set up so first we're going to need to use get version control now using git and version control is considered a best practice by just about any programmer out there so I think it's great that this is how Heroku does its deployments because it basically steers you towards best practices so installing git is also very straightforward so I'll open up the git download page here now most of you likely already have git installed so I'm not going to go into that here but if for some reason you don't have git installed then I'll leave a link in the description section below to this download page that I have pulled up here so that you can download git for whichever operating system you are using now another thing that you're going to want to do once you have a Heroku account is you're going to want to install the Heroku command-line interface which is the Heroku CLI for short the Heroku CLI is going to allow us to deploy our application from our command line using some Heroku specific commands so this page that I have opened here will give you instructions for how to download this for a Mac Windows or Linux and again I'll have a link to this page in the description section below as well so that you can download the CLI for whichever operating system you're using I'm on a Mac so that's the option that I chose but I've already downloaded and installed this and once you have that

### Testing the Heroku CLI [3:33]

CLI installed let's open up our command line and see if that worked so I'm going to open up my command line here and you're just went going to test this by simply typing in Heroku so I'll type in Heroku and run that and if our installation of the Heroku CLI worked then it should list out a list of commands that we can use to interact with our account now the first thing that we'll need to do is log in through the command line so that we're validated to run these commands on our account so to do this I'm just going to scroll down actually I'll just clear my page here by the way by typing in Heroku you can see all of the commands that we can use here but I will clear the page now to login I'm going to say Heroku login and I'll run that and you can see it says press any key to open up the browser to log in or press Q to exit so I'm going to press any key and it's going to open up a

### Logging into Heroku [4:27]

browser and asked me to login to Heroku so from here you can log-in to your Heroku account so I'm going to go ahead and do that okay so now it says we're logged in and that we can close this page and return to our CLI and that it should now be logged in so now I should be authenticated so once you're authenticated then we can continue with our deployment so there are a few things that we want to do within our project before we deploy so I'm going to pull back up my terminal here and I've already

### Requirementstxt file [5:00]

navigated to my project directory and you're going to want to do the same mine is located on my desktop and it's called Jango Project but you can navigate to wherever yours is located and once you're navigated to your project then we're going to want to create a requirements txt file now if you haven't created a requirements that txt file basically it is a file that holds all of the dependencies that we're using in our application now I'm using a virtual environment so all of my packages are specific to this project but if you aren't using a virtual environment then I'll link to the requirements that txt file that my app produces and I'll link to those in the description section below now you likely don't want to just use your systems Python dependencies because it could contain a lot of packages that aren't specific to your project and all of these are going to get installed on Heroku so it's best to keep it exclusive to what's actually needed for our application so first before we create our requirements txt file of our dependencies let's pip and install one more package that we're going to use when our application is deployed and that is G unicorn so I'm going to install that by saying pip install G unicorn so we will install that and once we have that installed now we want to create our requirements that txt file now the requirements file is also how Heroku knows that it's a Python application if you're using a setup PI file or a pip file then you can use those instead and it'll also recognize it as a Python application okay so now to make this requirements that txt file we can simply say let me clear this and first do something called a pip freeze if I run pip freeze then we can see that this lists out all of the PIP installations that we have currently made and these are what we want to add to a requirements txt file so if you're on Windows then you can just create a requirements txt file in your project and copy all of those copy all of the output from pip freeze and just paste that into a requirements txt file if you are on Mac or Linux or using bash then we can simply say pip freeze and we can just pipe that into a requirements dot txt file and that'll create that file for us with the output of that pip freeze command okay so now we should have that requirements dot txt file in our project folder that has the output from that pip freeze command okay so another thing that we need to do here is to use git within our application so some of you may have already been using it for this but if not then I'll run through these steps real quick so it's extremely easy to do we just have to simply run the command to get an it to initialize and empty git repository so we ran that and we can see it says initialize empty git repository in our project folder so now we're using git version control within our application it's actually that easy in this video we're going to be using very simple git commands so you don't need to be extremely comfortable with using it in the command line but if you'd like to see an in-depth explanation and walkthrough of how to use git then I do have a detailed video on that as well and I'll leave a link to that in the description section below if anyone is interested okay so now we're ready to tell git what files we want to actually track there are usually going to be certain files that we don't want get to track so for example anything that is specific to my machine or something like that we're going to want to ignore that now one resource that I use a lot for this is from github itself and it's a template of commonly ignored files for Python programs and actually have that open here in my browser let me close down this page and open this up so this is the Python get nor file here and we can see that it ignores files like pyc files and logs and stuff like that kind of stuff you likely don't want to track with git it also has a django section if I scroll down here a bit we can see it has a django section here now we can see it ignores our DB sequel Lite file as well and that is our sequel Lite database and that's fine because we're actually not going to be using sequel Lite for our production database in development we've been using sequel Lite but for production we're going to set up a new Postgres database Postgres is the database of choice for a lot of different websites because of its you know features and performance so that's what we're going to be using and it's also what Heroku recommends and it's walkthroughs so I'm actually going to copy this template get ignore file from github and use that as a starting point for my get ignore file now one tip is that if you're ever copying something from github then you can just click on this raw link here towards the top right of the page and it will take you to just the text of the file so that it's easier to copy and paste so this is all of the text of that git ignore file so I'm going to just highlight all of that and copy and now I'm going to go back to my project and in my project I'm going to create a git ignore file so I'm going to go to a new file and this is actually going to be a dot file so it is dot get ignore so don't forget the dot before get ignore so I'm going to create that and open that up and I'm going to paste in what we got from github and then scroll up here to the top so we can see that it is ignoring a lot of these different files we have some unit test files in here and things like that so that's good now at the top of mine I'm also going to add something that gets added on Macs sometimes if you're on Windows then you don't have to do this but at the top I'm also going to add a section for Mac and I'm gonna add a file here that is dot d s underscore store and that's just an annoying file that gets created on Mac sometimes that we don't want to commit with our project as well so I'm going to add that to the get it ignore file okay so now that we have our

### git commit [11:12]

get ignore file let's go back to the command line and commit all of our changes so far so I'm going to open up our terminal here and to do this we can simply say git add with a dash capital a and that's going to add all of the changes that we have so far so I'll run that and now if I do a git status then we can see that we have all of these new files now the reason our entire project is saying that it's new files is because we just initialized our git repository so all of these files are new to get none of them have been committed yet so now let's commit them to get so I can say git commit and then we can do a dash M here to set a message for this commit and I'm just gonna say initial commit and run that okay so now we've committed these files to get locally so now that we've committed these locally let's create an Heroku app where we can push this code so to do this we can simply use the Heroku command so let me clear the screen here so I can say whoops Heroku oh sorry about that Heroku create and now the name of our app so I've just called mine my awesome app and run that well and it looks like my awesome app is already taken as a name for a Heroku app so instead this might take some trial and error but let's see here I'll call this my Django app and try to create that that's already taken as well okay how about this we'll do my awesome Django app I doubt that is taken okay so that one worked okay so when that app is created it's actually going to create a domain for us as well and I can see that domain and the output of that create command you could so you can either copy and paste that into your browser or you can simply run Heroku open to open that in your browser so this is the URL here you can copy and paste that into a browser or let me clear my screen here or I can just say Heroku open and it will

### Heroku open [13:20]

automatically open that URL in my browser so we can see here that our site is currently working but it's just using the default Heroku site for now if that Heroku open command didn't work for you and it says that you're missing a require flag to specify what app you want to open then it's likely because you're not in your projects directory within your command line or possibly you did some of these steps out of order so for example if you create an app in a directory that isn't tracked by git then it might give you some issues and require you to set that stuff up in a different way but if you did everything in the order that we covered in this video then this should be working for you so we can see here that it gave us a domain of my awesome Jango app dot Heroku app comm and we currently have Heroku default site so we haven't actually deployed anything yet so now let's go ahead and do that so let's push our code to Heroku so if we go back to our command line then we can push that by saying git push Heroku master so if we run that then this is going to start pushing our code up to Heroku so we can see that it's going to do a lot of different things here so it's saying that a Python app is detected that it's installing Python installing pip and all of these different things so let me let's run through this installation here ok so if you just have a simple Python

### Troubleshooting [14:45]

application then that might have worked for you and pushed that up to Heroku now ours didn't work because our application is a bit more complex and it ran into an issue while it was deploying now the good part is that if it runs into an error like this then your app just doesn't get deployed so it won't break your website if it runs into errors at this deployment section here so if we scroll up here a little bit then we should see a detailed traceback of exactly what went wrong now my text is a bit larger here so that you all can see so this is going to be a bit bunched together but I'll see if I can find the part where it ran into an error so we can see that the error happened when it ran this command Python managed PI collect static now if that's not obvious from this section here if I scroll down a little bit here then we can see that it gives a note here error while running Python managed dot pi collects static so what that command does is it collects our static files so that they can be served up in production so if you've been following along with this series then you might remember that at one point I mentioned that those static files would be handled differently in production so that's what this is so that's a command that Heroku runs for us so that it can automate that process but if I scroll down a bit to see what went wrong with that command or scroll up a little bit then we can see that it says here you're using the static files app without having set the static route setting to a file system path so that's telling us that we don't have this static route variable set to where our static files will be located on the file system so let's go ahead and set that in our settings so I'm going to open up our project here I'm going to

### Setting up the static URL [16:35]

close the get ignore file and now let me open the settings dot py file and once when you have that open I'm going to scroll down to the very bottom here and let me turn off the word wrap so that we can see this a bit better now I'm going to scroll up to where we have our static URL okay so now let's set our static route now the location that Heroku will put the static files for us when they run that command is going to be in a directory called static files so we will say and let me actually copy this media root because this is going to be very similar but instead of media root this is going to be static root and the location that Heroku is going to put these files is going to be called static files so I will put that there and save that okay so earlier our deployment failed because we didn't have the static root set so now we can try to commit our updates and deploy this again now I'm gonna tell you right now that it's still not going to work but the reason I'm showing you one step at a time like this is so that we can see the justification for each step instead of just ploughing through all of the changes that we need to make seeing it one step at a time like this might help you debug an applic in the future if you have an application that is different from what we've created here so let's commit these updates and try to redeploy so I'm going to open back up the command line here and let me clear my screen and now let's commit these updates and redeploy so first if we want to see what we changed I can say get status and we can see that we modified our settings dot py file and to add that to the files that we want to commit we can simply say git add - a - add all of the modified files and now we can commit that so I'll say git commit dash M for a message and I'll just say updated static root and I will commit that okay so now we have committed that locally and now let's actually push that up to Heroku and try to deploy that so I'm gonna say git push Heroku master so if I run that then it's going to run through and try to deploy this again okay so it looks

### Running Heroku Open [18:52]

like our deployment was successful and we don't have any errors when we tried to push our code so that's good but now let's try to open our application in the browser and again if I clear my screen we can do that with Heroku open so I'll run Heroku open to open our application in the browser and now let me make this a little larger here so that you all can read it so now it's telling us that we're getting an application error so this is different than getting an error during deployment so it gives us a hint here it says that if we're the owner of this application then we can run this command Heroku logs - tale that command will allow us to see the latest logs of our application so I'm going to copy that command and let's go back to our terminal and run that so I'll pull up the terminal and I will paste that in Heroku logs - tale now if you look through these logs then we can actually find a lot of interesting things and you can actually use the logging module from the Python standard library to log out different things wherever you want so if you want these logs to be even more detailed and then you can use that but what we want to look at here is the air that we're getting so we can see that is right here at the bottom and it's running on two different lines here but we can see that the description of this air says that there are no web processes running so what's going on here is that we haven't told her Oh COO how we want to run our application so in order to do this we're going to need to create something called a proc file so I'm going to create this and explain along the way so in the root of our project directory I'm going to create that proc file so within our project directory I'm going to say new file and this is going to be called proc file so that's an uppercase P and then lowercase for the rest and that's PR OC f IL e so this proc file is going to be a simple text file now even though it's a text file be sure you don't create the file with a dot txt extension it should not have an extension so I'm going to create that file and then open that up now within this file we're going to create that web process that our logs said wasn't running so to do this we can simply say and I'm going to type this all out and then explain it later so I'll say web G unicorn make sure I spelled that right G unicorn and then Django underscore project dot WSGI okay so now let's look this over a piece by piece so web is the process type the name web is important here it declares that this process type will receive web traffic when deployed now G unicorn is the command needed to run our web process if you don't know what that is it's basically a way for our Python code to talk to the web server and it's going to do that through our WSGI file so WSGI stands for web service gateway interface so G unicorn is the command and then we're specifying the location to the WSGI file in our application after that so Django project is my package and you'll want to have that be whatever your project directory is called so this right here should be the name the same name as the directory that holds your Settings dot py file so if you called that you know my awesome app or something like that then you want to put that here I called mine Django project and after we specify that package then we want to specify our WSGI file now we didn't make any changes to that WSGI file in this series but that file has a variable called application and g unicorn we'll use that to run our application for us okay so that's going to do it for our proc file now these files can contain additional process types so for example you know you might declare one for a background worker process that processes items off a queue or something like that but in this video we're only going to do the web process so I've saved that file and now let's commit those changes and see what we get in the browser and again I can tell you that this still isn't going to work but we'll go step by step so I'm going to re open our terminal here and I'll press ctrl C to quit those logs and now we can commit our changes so again if we want to see what changes we've made we can do a git status and we can see that we have a new file here proc file so I'll add that and say git add - a to add all of our changes and now do a commit so get commit with a message of added proc file and now I'll clear the screen now let's push this up to Heroku so I'll say git push Heroku whoops Heroku master and run that okay

### Push to Heroku [24:03]

so now we've pushed those changes up to Heroku now you might have noticed that your push to Heroku was faster there than it was before and that's because Roku will see that we've already installed all of the dependencies from the requirements file before and that there aren't any new ones so it'll just skip that step so that's a nice feature of Heroku ok so now let's open up our application and the browser again and again we can do that with just Heroku open so I will run that and open this up okay so now we can see that we're getting a different looking error here now you might recognize that this is actually the Django debug error screen so we have made some progress this is actually now using Django now one thing that should worry us here is that it's showing us so much information on this debug screen and that's why we should definitely turn off debug mode when working in production people could use this information to find vulnerabilities in our application and we definitely don't want that but while we have the debug output here we may as well see what it says so at the very top here it says invalid HTTP host header with this domain name here and then it says you may need to add this to our allowed host so since this is now deployed Django has some built-in security features where we need to explicitly specify the host that we're going to allow for our application and we're going to make this change in our settings dot py file so I'm just going to copy that host that it says that we need to add so that is right here mine is my awesome Django app dot Heroku app comm I'll copy that and now I'm going to add that to our settings dot py file so I'm going to open up our application go back to our settings dot py file and scroll up here to the top and if we scroll down a little bit we can see that we have this allowed host section here so I'm going to paste that in as a string so my awesome Django app dot Heroku app comm now we can also see here in our settings that we have debug equal to true now like I said before we're going to want to set debug equal to false in production but we shouldn't just set it equal to false because when we're on our local machine we actually want debug equal to true for testing so anytime you need a value that's specific to whichever machine you're on that is a good candidate for an environment variable so if you've been following along with this entire series then you should already have some experience setting environment variables since that's where we put our username and password for our email service and also our credentials for AWS s3 but if you don't know how to set environment variables then I actually have a separate video on how to do that for your operating system so I'll leave a link to that video in the description section below if you need to give that a watch so I'm actually going to set a debug environment variable here so that can be different for our local machine and Heroku now while we're setting environment variables we should also move our secret key to an environment variable while we're at it so the secret key shouldn't be something that we put in our code potentially for other people to see so I'm going to create an environment variable for the secret key as well and I'm actually going to create a new secret key for our application since we've been using the current one for development so the new one that we create will be for production now there are a bunch of ways that we can generate a new secret key but the way that I like to do this is from the Python interpreter so I'm just going to open up my command line here and I'm going to run Python so I'm going

### Generate Secret Key [27:46]

to use Python here to generate a random key and you actually have to be running Python 3 in order to use this module so I'm gonna do this using a module called secrets and that is included in the Python 3 standard library so I'm gonna say import secrets and run that and that imported and now I'm gonna use a method called token hex to generate this and the more bytes that you pass in to that method the longer the random value will be so I'll say secrets dot token underscore hex and I'll just pass in a 24 for the bytes there and we can see that it generates this really long random string and that's about as long as our current secret key so I'm going to copy that and I'm gonna add that to my environment variables now since I'm on a Mac I can put these in my dot bash underscore profile file but like I said if you're on a different operating system and don't know how to create environment variables for your operating system then you can watch that other video that I mentioned just a bit ago that's linked in the description section below so I'm going to open my bash profile here so I'm going to exit out of the Python interpreter and open up my bash profile file in my home directory now I used a sublime command to open this up but if you don't have that sublime command set up on your system then you can just open that in whatever editor of your choice once we have that open let me turn off wordwrap here so that we can read this a little better once we have that open I'm going to set this secret key under this section here where we have our Django blog variables and you can see that we already have some environment variables here that we set for AWS in the last video okay so to set this secret key I'm just going to say export secret underscore key set that equal to that string that we just generated and now I'm also going to create an environment variable for our debug value so I'm going to say export debug underscore value and I'm going to set that equal to true since we're on our local machine and we want that debug value equal to true so that we can test our site now on Heroku we're going to set that environment variable equal to false since that'll be our production version now also notice here that our environment variable is a string of true and not just a simple boolean like we would see in Python and we'll see why that's important in just a second but we'll ignore it for now okay so now I have my environment variable set for my local machine so now let's set our environment variables for Heroku so let me save that before we exit out of it and I'm going to pull up my terminal to set these Heroku environment variables so I'm still within my project here within the command line now to add these environment variables to Heroku we can simply say Heroku config colon set and now we can set our environment variables so first I'm going to set a secret key and actually just to make sure I don't mess up any of the spelling here whoops let me go back to our bash profile and I'm just going to grab these from here and whoops copy those back into our terminal so Heroku config set and we'll set that secret key equal to that value and now also say Heroku config: set go back to our environment variables here and set this debug value equal to true and if you've been following along with this series then you also need to set a few more environment variables so these will be the username and password for the email service that you set up in the reset password video and also the AWS credentials that we set up in the AWS s3 video so first I'll do the AWS credentials since we already have those pulled up here so I'm gonna copy this access key ID so again let me just rerun that Heroku config set and I will set that to the AWS access key ID that we used and again let me now do Heroku config set and set this equal to our AWS secret access key okay and lastly let me do Roku config set this equal to our AWS Storage bucket name and paste that in got that set and now I'm going to do my email user and password for the email service so I'm going to say Heroku config set and first that was email underscore user and my email user is equal to Corey M Schaefer at gmail. com and for the password that was email underscore Paz now I'm not actually going to put my email password here for obvious reasons I don't want to broadcast that to everyone in a video but I'm going to pause the video and put in my password here in an environment variable to get the emails to work and then I will pick up the video after I have put that in okay so I pause the video and put in that email password as an environment variable and now picked it back up so now that we have all of those environment variables set now let's update our settings dot py file to use those environment variables that we just set so I'm going to go back to sublime and open up our settings dot py file so for our secret key here I'm just going to comment out the old secret key and now I'll set a new one here so I'll say secret key is equal to and to grab an environment variable we can say Oh s dot environed on git and we can get that environment variable and we just called that secret key as the environment variable as well so we can just grab that now for our debug value I'm going to leave this set to true just for a bit longer until we get this deployed and once it's working then I'll set it equal to false once everything is working properly okay so we've made some changes to our settings so now let's commit our changes and put those up to Heroku and see what we get so I'm gonna go back to the command line here and instead of running get status I'm just going to go ahead and add these you can run get status if you want to see what you've changed but I'll just add all of our changes and now I'll say git commit dash M for the message and we can say updated settings and now let's push that to Heroku so git push Heroku master and I'll run that okay so once we have that deployed let's take a look at our site so I'm going to say Heroku open to open that up in the browser okay so we're still getting an error this time the error says no such table blog post okay so remember we didn't push our SQL Lite database that we've been using in deployment because we're going to use Postgres in production so this is a little expected since we haven't created any tables in a Postgres database yet so first of all we're going to need Postgres installed on our machine to get this working so let me pull up Roku's documentation on this I I have this pulled up over here okay and again I'll have a link to all of these resources in the description section below so that you don't have to look around for this stuff so we can see that they give instructions for how to install Postgres for different operating systems so we have it to setup for mac and for windows and for linux now i already went through the installation for mac so you don't have to watch me do that but it's pretty straightforward just follow what they have listed step by step so once you have Postgres installed on your machine we're going to want to create a Postgres database on Heroku now it's possible that Heroku already created a database for us it does this for certain applications that have certain libraries so we can check that if I go back to the command line if I say Heroku add-ons and run that then we can see that we have a

### Heroku Addons [36:31]

database for our app already but if you do need to create a database then instead if it didn't create one for you then you could say Heroku add-ons: and create and then Heroku - PostgreSQL let me spell it correctly and then: and then you can put in the plan of database that you want the free version is Hobby - dev so if you don't have a database already then you can run that now I'm not gonna run that since it already has a database for me here now like I said that hobby dev part is the plan and that is the free tier but if you ever need any more performance then you can always bump that up and we can see more about that if we were to run Heroku PG we can see there it shows us a bit more about that database so we can see that it has for the rows zero out of ten thousand so the free tier does have a ten thousand row limit okay so now that we have a Postgres database now we actually need to create tables for it so we also need to create a super user where we can login to our Django admin site so first things first we need to add the credentials to our settings dot py file so that our django application can talk to the database now instead of doing this manually we're going to use a helper function that Heroku provides for us to do this and we can get that by running a pip install so here within our command line I'm going to do an installation of pip install janggu Django - Heroku so I'll run that so that Django

### Django Heroku [38:18]

Heroku package is going to automatically configure our database URL and we'll also take care of connecting our static assets to G unicorn using a package called white noise so it's going to set up some stuff for logging and things like that - I think this will even take care of a couple of things we already set up like our secret key and allowed host but I like setting those myself since I'm already in there anyways okay so the way that we get this to work is to open up our settings dot py file so let me open that up here and at the top of our file we want to import that module so I'm going to import Django underscore Heroku now I know that we installed that as Django - Roku but when we import its Django underscore Heroku now at the very bottom of our settings file so let me scroll all the way to the bottom at the very bottom we're gonna say Django Heroku dot Settings and we are going to pass in locals and these need parentheses here at the end so like I said just by adding that in that's going to automatically set a lot of those configurations for us okay so now let's deploy this now one thing that you don't want to forget to do is update your requirements txt file so we've pip installed some packages so we want to make sure Heroku gets those changes so I'm gonna grab those pip frees dependencies again and update my requirements dot txt file so to do this on Mac I can simply say pip freeze and pipe those in to requirements dot txt again if you're on a Windows then you can just paste that pip freeze output into your requirements that txt file instead ok so again if you want to see the changes you can run a get status now mine says that I've modified my settings dot py and that I haven't modified my requirements dot txt file and I think that's just because I already had that one Django Heroku module installed already so that was already in that file ok so now to add those I'll say git add dash a and now I can commit those changes locally so get commit dash M with a message here and the message I will just say added janggu - Heroku and now I can to push those changes to Heroku so I'll say git push Heroku master and let that run through the deployment again ok so that deployment

### Running the Commands [40:58]

is complete now I promised to y'all that we are about to have our application working but if we check it now then it should still give us some errors so let's see what those are so I'll say Heroku open and open up our page in the browser again and again it says relation and blog posts does not exist ok so we're still getting an error that our tables don't exist but we should be able to talk to the database now with Django so now we just need to run the commands that create the tables and while we're at it we'll also create a super user that has admin access now if you remember from the database video earlier in this series we can create our tables by running the manage py migrate command we shouldn't need to run make migrations because the migrations we already had should have been pushed up with our application so we only need to run migrate so how do we run that command on Heroku well there are two ways that we can do this we can if we want to run a command on Heroku then we can use Heroku run so for example let me go back to my terminal here and let me go ahead and clear the screen so if we want to run a command on Heroku we can say Heroku run and now I can specify any command that I want to run on the shell of our Heroku and that command will be run from the root of our application so I could simply say Heroku run Python managed py migrate so if I run that then it's going to run Python managed py migrate on our Heroku machine so that should have run

### Creating a Super User [42:45]

our migrations for our application and created our tables and if we scroll up here we can see that it looks like that's what it did we have a bunch of green marks here that said everything went ok ok so now our tables should be created now while we're at it let's also create a super user that can login to the admin panel remember we're starting over with a new production database here so our old one that we used in development it no longer exists we have to also create this on Heroku now another way that we can run in commands on Heroku is to actually run a bash shell from Heroku itself so let me clear my screen here now the way to do this is we can simply say Heroku run bash and if I run Heroku run at bash then it should give me a bash shell to our Heroku machine okay so now we're connected and now we can run some commands since we'll actually be on the hokum of machine we won't need to prepend the command with Heroku run anymore now these roca machines are called dinos and they are a Linux system so you need to run Linux commands so for example if I run LS then it lists the files and directories of the current directory which is the root of our application so we can run commands from here as well so to create a super user we can simply say python managed dot py and create super user so if i run that then it's going to ask for a username and password and all of that good stuff so I will make my username core EMS and then the email address put in my email address and now I password I'll just put as testing 321 okay so our super user was created successfully and now to exit our Heroku manned machine we can simply type exit and now once it exits out of there we are now back on our local machine now that we're let's try running our website again now that we should have created those tables and that super user so I'm gonna say Heroku open to

### Testing the Website [45:03]

open our website in the browser and now it actually looks like we're getting somewhere let me resize this here to be a little smaller so we can see our application is running here at my awesome django app roku app comm so this should be accessible to anyone on the internet at this point now there currently aren't any posts because we created a new Postgres database for production so let's test out some of the functionality of the site and see if everything is working so let's try to log in with that super user that we just created so the username there was cory ms the password was testing three two one so if i log in but it looks like we logged in because we can access our profile and stuff like that first let's go to the admin panel and see if our admin site is working and it is so that's good let me try logging out as this user and going back to our site up that's trying to log back into the admin site let's try to create a new user so i'll go to register for the new username I'll just say deployed user for the email I'll say D you at test comm password as testing three to one signup okay looks like that user was created let's try to log in as that user okay and that seemed to work so now let's create some new posts here so my first post with the content of my first post and that seems to be working well we are getting our images from Amazon s3 like we set up in the last video so that is working well also let me create another post here and try to do some updates so I'll create a post here click on that go to update and say my updated second post that seems to work well let me try to delete that post okay and that's working well also that second post was deleted so we're just kind of going through here and making sure that all this functionality is working correctly now this is also why it's a good idea to have tests set up for your website so that you can just automate all of these tests because doing this manually every time you make some updates takes a long time as you can see but I'm gonna try to update a profile picture here so it looks like our profile picture updated successfully so that's good and lastly let's try to log out and see if it sends us an email if we click on forgot password if this looks like it sends us a reset password email okay and we can see an email has been sent with instructions to reset your password so that looks like it is working also and I actually just saw that email pop up on my phone so it does look like that email went through okay so all of that seems to be working great so now we've got our application deployed to Heroku now when you're working with django has a deployment checklist on their website that you can look through to make sure that you did everything that you need to do when deploying applications so let me actually search for this so I will go to django deployment checklist and it should be the first result here okay so this deployment checklist it'll kind of tell you the things that you need to do so for example you know hiding your secret key setting debug equal to false so you're going to want to look through this deployment checklist and make sure that you did everything that you need to do when deploying applications and this actually reminds me so it's a good thing that we use this checklist actually reminds me that we didn't set debug equal to false yet we just had it set to true while we were debugging our application but now we need to set that equal to our environment variable so to change that let's go back to our settings dot py file here I'm going to scroll up to our debug now for our debug value here we have to do something a bit different so this has to be equal to a boolean of true or false but we can only get back strings from environment variables so instead I'm going to do a little trick to turn this into a boolean so if we use a conditional to test whether that environment variable is equal to the string of true then it will return a boolean of true or false so let me just do this and then I will explain what it's doing so within some parentheses here I'm gonna say OS dot environment and I'm going to get the environment variable and we called that debug value I'm pretty sure if I go back to my bash profile can check yeah that is debug value and that is set equal to the string of true so we'll say if that environment variable of debug value is equal to the string of true and if that then this entire conditional is going to return true and that's what debug will be set to so basically debug is going to be equal to false if the debug value environment variable is anything but true so that'll work well so now if we wanted to push that last change then we could simply open up our terminal and do a git add dash a of all of our changes and do a git commit with a message here and I'll set that as a message of set debug value and now you can push that to Heroku by saying git push Heroku master and run through that ok so now we have our site deployed to

### Conclusion [51:03]

Heroku now if you just want to have your site accessible and don't care about having that Heroku domain name then you could simply leave it like this and share that with anyone who you want to see your application now if you want a custom domain name then there is a bit more that we need to do but I believe I'll save that for a future video if anyone is interested in seeing how that's done so if that's something that you want to see then just let me know now there's also a lot more that we could learn about the Heroku platform itself and what all capabilities that are available to us so for example they have a great versioning system where it's easy to rollback if you ever make a mistake so for example let me just show a quick example here if I was to do Heroku and run the releases command then this will show us all of the deployments that we made in this video and all of these have a version associated with the deployment so we can see here that there is a V 16 at the top the 15 V 14 so if you pushed out a bad deployment and simply wanted to roll back to a stable version then all you would have to do in that case is so let me copy like V 15 here I could simply say something like Heroku rollback and then paste in V 15 so if I were to run that command which I'm not gonna do because I don't want to actually roll that back but that would take you back to that specific deployment automatically without any actual work on your end so I think that those are great features so if you're interested in learning more about Heroku then I would highly recommend going through their documentation to see what all you can do with their service ok so I think that is going to do it for this video hopefully now you have a good understanding for how you can push your Python application to Heroku but if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest ways to simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching
