We’ve successfully deployed our Django application to Heroku, but it’s not ready for public release yet! This video will explore using production settings files, pulling sensitive information out of our code repository, and using a more robust database for our app.
Django app has been successfully deployed to Heroku, but 0:00 it's not quite ready to present to the world right. 0:03 The debug stuff is still there, there's other problems. 0:06 I need to tweak some of the settings. 0:09 I need to pull some important settings into environment variables, 0:10 and I also need to connect the project to Heroku's Postgres database. 0:13 I'm gonna start by getting some production ready settings. 0:17 A common pattern in Django projects, 0:19 is to have a base settings file which is sometimes used for local development and 0:21 a production settings file, that's used in production, but 0:26 that pulls in all the base settings and makes whatever changes are necessary. 0:29 To make our lives easier, 0:34 I'm going to want our production settings to be importable as its own Python module. 0:35 So in your terminal, starting from the top of your get repo, 0:40 we're gonna create the module. 0:43 So mkdir djangoal/djangoal/ I'm gonna call this deploy_settings. 0:45 And then I'm gonna touch djangoal/djangoal/deploy_settings/__init_- 0:51 _.py. 0:57 So now this creates the module directory for the deploy settings and 0:59 the Dunder.init, 1:02 Dunder.py tells Python that it's a module so it makes it importable. 1:04 While I'm here I'm gonna go ahead and 1:08 create the wsgi file that I'm gonna use for deploying. 1:09 So I'm gonna copy djangoal/djangoal/wsgi.py 1:11 to djangoal/djangoal/deploy.py. 1:16 All right, so now over in my text editor, 1:20 I'm going to open up that deploy settings in net.pie and it's currently empty. 1:23 The first thing I wanna do is I wanna bring all of the standard settings in so 1:29 that I can then override them. 1:33 So from jangoal.settings import *. 1:34 And that imports all the existing settings. 1:40 In the last video, I should do the debug was still on, so the first thing I want to 1:41 do is I want to turn off debug, so debug needs to be equal to false. 1:45 And I'm also gonna set template debug equal to debug, 1:49 I can set it equal to false but making it where they point to each other is 1:54 easier because I only have to change this one to change both of them. 1:58 And you notice, this is here because of DEBUG=True. 2:01 The TEMPLATE_DEBUG does some other stuff as well. 2:06 Okay, so we check our work, we can deploy it with the new settings. 2:11 So we need a wire those settings into the wsgi file. 2:16 So I'm gonna open up the deploy.py, the settings or this wsgi file rather. 2:20 And I'm gonna change the OS.environSetDefault 2:25 to be the deploy settings. 2:29 So this line right here sets what is used as the DJANGO settings module. 2:33 So I'm gonna tell it to use the deploy settings module. 2:38 I also need to change the proc file to point Heroku at the right wsgi file. 2:43 So, cuz right now it goes to djangoal.wsgi. 2:47 And it needs to go to djangoal.deploy, not .wsgi. 2:51 Okay, so to go over the changes real quick. 2:55 We created this directory and the __init__.py, we added in this Import for 2:58 all the settings changed it to false and false in the deploy wsgi. 3:04 We told it to use the deploy settings and in the proc file. 3:08 We told us to use the deploy wgsi. 3:12 All right so I need to add these settings or these changes, 3:15 git add dot, git commit -m, add production settings. 3:20 And then git push heroku master. 3:26 And now once Heroku has built and deployed the app, I'm going to go ahead and 3:31 open it back up in the web browser. 3:35 Okay, so now that is deployed I'm going to try and open this up in the browser. 3:42 Take off the doesn't exist. 3:46 And okay. 3:49 So we know debug is off, because I'm not getting the error page. 3:50 But it's a bad request, right? 3:56 It's a bad request error. 3:58 So something's going wrong here. 4:00 And what is that? 4:03 Well, what's going on is that when we turned off debug on the project. 4:03 Which we did through here, right? 4:08 We turned debug off. 4:10 Django assumes that we're now running in a production environment and so 4:11 Django turns off some of, or rather it turns on some additional protections. 4:14 It turns off some places where it was a bit lax. 4:19 One of those protections is only allowing domains in the allowed host setting to 4:22 access the site. 4:27 So when you request a site, there's a HTTP attribute 4:28 that comes through which is the refer or the requesting site. 4:33 And if the requesting site isn't in the allowed hosts Django doesn't allow that 4:38 request to go through. 4:42 So we need to add some allowed hosts. 4:44 So allowed hosts Is in a list. 4:49 And I'm gonna add two things in here, I'm gonna add localhost, 4:53 which should be lowercase, and I'm going to add .herokuapp.com. 4:58 Now I'm keeping I'm adding a local host in here just in case I want to do any local 5:05 testing with production settings that way I'll still be able to request the site. 5:10 And the .Herokuapp.com is so that any Heroku app subdomain will 5:14 be allowed access to the site so notice this Django.Herokuapp.com. 5:19 That's what I want .Heroku app. 5:25 Okay, so once again I'm gonna add these settings in. 5:27 And I'm going to push to heroku. 5:39 So now that it's deployed I'm going to check the website one more time. 5:46 Nice, I got the website again. 5:52 Now, if I try to go to a bad location, like again, the bad location. 5:54 Then now I get a pretty standard 404, right, a generic 404 page. 6:02 The next thing I wanna do is to get the app a little bit closer to production 6:07 ready by pulling the secret configuration out of our settings file. 6:10 If you look in the base settings file, the settings.py, 6:14 up here near the top there is a key called the secret key and it's in our repo. 6:19 Anyone else that has access to the repo could see this. 6:26 Now that the debug is turned off and 6:29 Django's not gonna leak any environment configuration variables on errors. 6:32 I need to create a new secret key and store that in an environment variable 6:38 on Heroku and then have the settings read that key from the environment. 6:41 So first I need a new secret key. 6:45 So in my browser I'm going to go to 6:48 miniwebtool.com/django-secret-key-genera- tor. 6:52 And this is a page that, well as you can probably guess generates secret keys for 7:00 Django. 7:04 So, I am going to click generate secret key. 7:06 I'm gonna copy of this key and just hold onto it in my clipboard. 7:09 Now, I'm gonna go over to the Heroku Dashboard for my app, 7:14 actually I'm gonna close that. 7:17 I'm gonna over here to the Heroku Dashboard for the app and 7:20 I'm gonna come up to the Settings tab. 7:24 And if you remember this Reveal Config Vars, 7:26 this is where I'm going to go ahead and add this. 7:28 So I'm gonna add a new key here that I'm gonna call SECRET_KEY. 7:30 And I'm going to type in the secret key, or paste in, and then I'm gonna click add. 7:34 Be sure you click add. 7:41 If you don't click add, it won't actually be set into your environment. 7:42 So now there's a new environment variable there. 7:46 Now while we're here I'm going to set one more setting that I'm going to need 7:50 for the app. 7:53 In the key field here I'm going to type in Django settings module. 7:54 And for the value, I'm going to set djangoal.deploy_settings. 8:00 And that will tell the Django project when it's being built that it should use our 8:07 deploy settings rather than the base development settings. 8:12 So now the secret key is set in the Heroku environment. 8:16 So we need to read it from the settings file, so back over in the text editor. 8:19 In base setting.py I want to add a new function that gets values from 8:24 the environment into the settings. 8:28 So I'm gonna add a new function here that's name 8:30 def get_env_variable and It's gonna take a var_name and 8:36 it's gonna do try return os.environ[var_name]. 8:42 Unless there is a KeyError and then we're gonna 8:48 have an error message, set the env variable 8:54 And then if debug is true. 9:02 I'm going to do warnings.warn with the error_msg and 9:05 otherwise I'm going to raise ImproperlyConfigured with the error_msg. 9:11 All right, so this function will try to get an environment variable. 9:24 Whatever name that I pass in and it'll throw an error or 9:28 a warning if that variable isn't there. 9:31 I like this function being in the base settings file so 9:34 that any settings file that I create has access to it. 9:38 Back over in the Deploy settings, I'm gonna use this to get the secret key. 9:41 So SECRET_KEY = get_env_variable("SECRET_KEY"). 9:47 So now I'm gonna try deploying this and make sure that everything worked. 9:54 So I'll do a git add. 9:58 git commit -m add env var getter. 10:00 And then git push heroku master. 10:07 And you'll know right away if something failed, like, 10:14 say if the the build phase failed, because collect static needs to find 10:18 the secret key in order to be able to do its job. 10:23 So if that passes then you know you're good. 10:26 Okay, so the last thing we need to do is to connect the project 10:29 to the Heroku postgres database. 10:34 We'll need to install two packages into our project to start with. 10:38 So we need to install DJ database URL and psycho PG2. 10:41 D.J. database U.R.L. is going to handle pulling the database U.R.L. 10:45 out of the environment variables and formatting it to play nicely with Django. 10:49 Psycho P.G. 10:53 two is the database driver that Django will use to connect to postgres. 10:54 So here in the terminal and I go ahead and do, pip install dj-database-url. 10:58 And I wanna do a version 0.4.1 and 11:06 psycopg2 version 2.6.2. 11:10 And this will actually fail if you don't have postgres stuff installed. 11:18 So I'm not gonna worry about this for the local but 11:23 I am gonna go ahead and add it to my requirements. 11:26 So I'll add dj-database-url==0.4.1 and 11:30 psycopg2==2.6.2. 11:37 If you have postgres installed locally, you can go ahead and 11:40 do the installation yourself there. 11:43 So now I need to make one change in the production settings file and 11:46 then I'll be ready to push. 11:51 So I'm gonna go over here to the init.py and I'm gonna add a couple of lines here. 11:52 Up at the top I'm gonna say 11:57 Import dj_database_url. 12:02 Down at the bottom here, I will do db_from_env = dj_database_url.config. 12:08 And then I'll do DATABASES['default'].update(db_from_env). 12:16 So these two lines will pull the database configuration in from the environment and 12:25 then override the default database that set up in the base settings. 12:30 So now it's time to deploy this much more production ready project. 12:34 So, I'm gonna do git add. 12:39 git commit- m add postgres db settings, 12:42 and then git push heroko master again. 12:47 So, everything should deploy successfully. 12:54 Okay, so everything deployed successfully, 12:59 and the app would be reading from the Heroku Postgres database, but 13:01 we currently haven't run any of our migrations on Heroku's database. 13:04 Now, I can fix this by shelling into the Heroku app. 13:08 Heroku allows limited shell access for running the application, for 13:11 executing one-off commands like the database operations, things like that. 13:15 So in the terminal you want to do heroku run bash -a and then your app name. 13:19 Whatever your app name is, so my case it's gonna be Djangoal and 13:26 you'll see it doing it's little connecting thing. 13:30 And then now that I'm here I can do python. 13:34 Actually, let me go into Djangoal. 13:38 All right. 13:42 Python manage.py migrate. 13:43 And then I'm gonna say that the settings 13:47 are equal to djangoal.deploy_settings. 13:52 And that runs all the migrations. 13:57 So now we have the right table. 13:59 We should probably go ahead and create a super user as well. 14:00 So Python manage.py createsuperuser and 14:02 again --settings=djangoal.deploysettings and 14:06 then you can set your username. 14:12 You can set your email address, you can set your password twice and 14:20 your Superuser should be created successfully. 14:24 So now if I go to my web app. 14:28 I don't need that SECRET_KEY generator anymore. 14:32 The app should still be up and running, which it is, and if I was go to the admin. 14:37 Don't have any of those files? 14:46 All right let's see if I can login. 14:49 I can, the database is working but those static files are missing. 14:54 I think in the next video we'll get the static files working, 14:57 get everything configured for static assets. 15:00 And I'll provide you with some tips on where you can go next with the deployed 15:02 Django project. 15:06
You need to sign up for Treehouse in order to download course files.Sign up