Our application is now running on Heroku, with a robust database backing it and our settings correctly adjusted for a production environment. What comes next? This video will cover better static asset deployment through WhiteNoise, as well as suggestions for making your deployment life better with Heroku.
So the Django project is now running on Heroku. 0:00 Has a robust database backing it, and 0:03 the configuration is all safely stored in environment variables. 0:05 What's left now is correctly configuring the static file setup, and 0:09 deciding what extra pieces we need to have before sharing our app with the world. 0:12 Let's start by getting our static files serving correctly on Heroku. 0:16 There's a couple of options here. 0:20 You can either serve the static assets directly from Heroku, or 0:21 you can move your static assets to a third party host like Amazon's S3 service. 0:25 Both have pros and cons, and if your app starts getting a ton of traffic, 0:30 moving static assets to something like S3 can have large performance benefits. 0:34 Since we're just starting out though, 0:38 starting from Haroku seems like a fine choice. 0:39 And that's what I'm gonna set up. 0:41 So in your terminal you need to install the white noise package and 0:43 we're going to save it to requirements.txt as well. 0:47 So let me get out of here cuz this is still 0:51 inside Haroku, pip install whitenoise. 0:55 3.2.1 double equals and then I'm gonna make sure and 1:00 add that into the requirements file. 1:06 There's that. 1:16 Now whitenoise is a Python package that's designed to make 1:17 serving static assets easier. 1:20 It's designed to work well with content delivery networks and it provides benefits 1:22 like compressing content and setting correct caching headers. 1:25 It's the Heroku recommended package for serving static content. 1:29 Now that we have it installed, 1:33 we need to link it to our application in two different places. 1:34 So in the settings file, the deploy settings. 1:38 We're gonna add one new line down at the bottom, which is STATICFILES_STORAGE. 1:43 Which is going to be equal to 1:48 whitenoise.django.GzipManifestStaticFiles- Storage. 1:51 This tells our project to use whitenoise as static file storage for 1:59 storing static files and finding static files and all of that stuff. 2:03 Next, I wanna open up the deploy.py and 2:08 if you remember this is our deploy ready wsgi file. 2:11 And I'm gonna add a line up here at the top and 2:15 I'm gonna say from white noise .django import DjangoWhiteNoise. 2:20 Then down here after the application equals get wsgi application I'm 2:27 gonna add a new line, where I'm gonna to wrap the application that I got 2:32 from the get wsgi application function. 2:37 With the DjangoWhiteNoise app so application 2:39 is going to equal DjangoWhiteNoise application. 2:44 And I just noticed that I misspelled import so 2:50 don't do that spell import correctly and then save your file. 2:52 So both those files are saved back in the terminal. 2:57 I need to go ahead and push those so git add dot, 3:00 git commit dash m add whitenoise to project and 3:05 git push heroku master. 3:10 Now once the project is done building and deploying, I'll be able to go to my 3:15 web apps URL in a browser and I should see that the site looks better. 3:19 So now that it's deployed, I like to test this by going and double checking 3:25 the admin, which before when I logged in looked like this and now when we refresh. 3:30 We get an exception. 3:39 And I got the exception because I have a typo in my deployed.py. 3:45 So let's go fix that. 3:50 So I go back over to my editor and I look at deploy.py, 3:52 I typed whitenaise instead of whitenoise. 3:56 And some going to git add dot git ci dash m typo. 4:00 Sorry git commit dash m typo and git push heroku master. 4:05 Typo's like that will bite you quite often. 4:11 They happen to me a lot so double check everything especially 4:17 when you're working with Heroku or any other host. 4:22 Okay, now, that's the deployed again, I'm gonna go and 4:26 double check the app and there we go. 4:31 Everything looks correct we have all the C.S.S. 4:34 that we're supposed to have in the admin. 4:35 So congratulations again. 4:38 You've successfully deployed your Django project to Heroku 4:40 with a good separation of configuration and code, a stable data base backend, and 4:42 proper serving of static assets. 4:47 Your site is now ready to use and share with the world. 4:48 Now, from here you have a whole world of operations of what to do next. 4:52 You can start using and sharing your site as is, 4:57 but you might want to add a custom domain to your site. 4:59 And you probably should add HTTPS if you're having users create accounts 5:01 and log in. 5:05 Every domain and HTTPS setup is slightly different, but 5:06 I can provide some general tips on getting this working for you. 5:10 With adding a custom domain, make sure to add that domain to your app, 5:13 either in the Heroku dashboard or the Heroku toolbelt. 5:16 So that Heroku knows how to route the domain to your app correctly. 5:19 You'll add a C name from your DNS provider to your Heroku app URL, and 5:22 Heroku will pass through traffic to your app. 5:26 One common gotcha is forgetting to add your new domain name 5:29 to your allowed hosts. 5:31 If we we were adding django.com to our site, our allowed host would 5:33 look something like this In the settings we'd have localhost, .herokuapp, 5:39 and then we'd also wanna have .djangoal.com. 5:45 That way any subdomain we added for djangoal would come through. 5:50 Now speaking of DNS providers, some DNS providers, or 5:54 domain registrars, will make it easy to get set up with HTTPS. 5:57 If your DNS provider, or registrar, 6:01 makes it easy to get an HTTPS certificate, then you can use the Heroku SSL add on. 6:03 Which I've linked to in the teacher's notes. 6:08 The Heroku SSL endpoint, it costs a bit of money, 6:10 it costs $20 per month, per Heroku app that you add it to. 6:13 But you get to use your own certificate and have that level of trust. 6:16 If you wanna get going quickly with HTTPS and you're okay with trusting your DNS 6:20 provider using Cloud Flare for DNS gives you free HTTPS on your domains. 6:24 The catch is that the HTTPS stops at Cloud Flare. 6:30 So the connection between Cloud Flare and Heroku is still regular HTTP. 6:32 This is probably okay for most apps especially if you're just getting started. 6:36 But it's something to be aware of. 6:40 Once you have domains and HTTPS set up, where do you go? 6:42 Well my next step is often using Heroku's Pipelines feature. 6:46 So that I can test against a staging. 6:49 Let me show you where that is, it's here in Deploy, there we go. 6:51 So that I can test against a staging. 6:56 Like a staging server, before pushing my project to production. 7:00 In the Heroku dashboard here in the deploy tab, there's a button for 7:05 this new pipeline. 7:09 You can click that button, and change the pipeline name if you want, you can leave 7:10 it as the name of your project anyway, and then you can click create pipeline. 7:14 And now you're gonna be taken to this pipeline overview screen 7:19 where you'll see that the app is listed here as the production app, and 7:22 the staging area is empty. 7:27 If you create a new app by following the instructions back from video one, and 7:30 so the what is it, 7:34 out here on the personal apps thing you'd have a thing here that says create app. 7:38 If you do that and you create your app. 7:43 Then you can add that here as the staging step of your pipeline. 7:45 So you probably name it like Djangoal staging. 7:49 You'll need to update your get remote to point to the staging server and so 7:52 now you deploy to staging instead of production and 7:55 you can test your app before your users see it. 7:58 Then when you're ready to deploy to production, 8:01 you can go to the pipeline overview screen which is this one here and 8:03 promote the build from staging to production. 8:06 Heroku will take the exact build that's on staging and deploy it to production so 8:09 you'll know exactly what you're getting. 8:13 From here, there's still so much you could do to give you peace of mind and 8:15 make your life easier like adding logging or error tracking or app metrics. 8:19 And you should definitely feel free to explore what Heroku and 8:22 their adon partners offer. 8:26 For now though I'm gonna leave the rest of that to you and 8:27 I'm gonna go get on to other stuff. 8:30 Thanks for watching the series and have fun deploying your Django apps to Heroku. 8:31
You need to sign up for Treehouse in order to download course files.Sign up