Locked lesson.
About this lesson
In this lesson, we'll continue to configuring our app for heroku by making more modifications, including: modifying Settings.py, adding Whitenoise Middleware, and creating the Requirements.text file.
Exercise files
Download this lesson’s related exercise files.
Modify Settings.py add Whitenoise Middleware and Create Requirements.text file.docx60.5 KB Modify Settings.py add Whitenoise Middleware and Create Requirements.text file - Solution.docx
58.8 KB
Quick reference
Modify Settings.py add Whitenoise Middleware and Create Requirements.text file
After installing those Heroku-required libraries, we need to add them to our settings.py file.
When to use
Do this anytime you want to host your app on Heroku.
Instructions
After pip installing each of the libraries mentioned, create a requirements.txt file at the terminal:
pip freeze > requirements.txt
Finally, add these lines to your settings.py file:
import django_heroku
import dj_database_url
from decouple import config
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
…
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
django_heroku.settings(locals())
Hints & tips
- pip freeze > requirements.txt
- Change your settings.py file to reflect the new libraries
- 00:04 All right, so we're about halfway done with configuring our app for heroku.
- 00:07 So now let's go to our settings.py file it's located in the django website
- 00:12 directory.
- 00:12 And we need to actually import all those things we just installed.
- 00:16 So I'm going to get rid of these comments and we just go import django_heroku.
- 00:21 That was one of them, another one was import dj_database_url.
- 00:28 And then finally this python decouple it's a little bit
- 00:33 different we go from decouple import config, okay?
- 00:37 So go ahead and save that, okay?
- 00:39 Now we've got to make some changes to this file as well.
- 00:41 So come down here to MIDDLEWARE, and we need to add something called whitenoise.
- 00:46 So let's just add another double single quotation marks.
- 00:49 So inside of here, type
- 00:54 in whitenoise.middleware.WhiteNoiseMiddlew-
- 01:03 are.
- 01:05 And whitenoise deals with static files, all of our images,
- 01:09 all of our CSS files and all of our JavaScript.
- 01:11 It kind of compresses it for heroku and it makes it usable up on the web.
- 01:16 So okay, we're almost done,
- 01:20 we've got a couple more things to do.
- 01:25 Down here at the very bottom,
- 01:30 we just type in all capital STATICFILES_STORAGE
- 01:38 = 'whitenoise.storage.CompressedManifestSta-
- 01:46 ticFilesStorage.
- 01:51 Okay, and then under that we need
- 01:56 to go django_heroku.settings and
- 02:02 then (locals(input)).
- 02:07 And this just tells the program to use the django heroku file that we imported.
- 02:14 And it's going to do everything we want behind the scenes so,
- 02:16 go ahead and save this.
- 02:17 One last thing, we need a requirements.txt file.
- 02:20 And you'll notice, remember I told you you can always see what has been installed in
- 02:24 your virtual environment by typing in pip freeze.
- 02:26 Well, we need to tell heroku that all of these things have been installed.
- 02:31 So to do that, we just type pip freeze > requirements.txt.
- 02:39 So we're going to create a file called requirements.txt, and
- 02:42 it's going to output all of our pip free stuff.
- 02:44 And so we can come back over here.
- 02:46 And we can see down here under the proc file, sure enough, this has been created.
- 02:49 And it has all the stuff that's pip frozen into our account,
- 02:52 all the things that have been installed.
- 02:55 So we see Django has been installed and heroku, dj-database-url,
- 03:00 psycopg2 that's the postgres database that heroku needs by 100 couple.
- 03:07 Remember that PITC from the very beginning of the course, when we installed Django.
- 03:10 Also the SQL parse from the very beginning and also whitenoise, which, like I said,
- 03:14 deals with our static files.
- 03:16 So that's it,
- 03:16 that's all the configuration we have to do in order to push our code to heroku.
- 03:20 And will actually push the code in the next video.
Lesson notes are only available for subscribers.