- [02m] 🏆 Objectives
- [03m] 🤔 Why You Should Know This
- [40m] 📖 Guided Tour: Deploy Tutorial on Heroku
- [10m] 🌴 BREAK
- [40m] 💻 In Class Activity: Deploy MakeWiki
- 📚 Resources & Credits
- Demonstrate a Django deployment illustrated live, step by step, with guided mini activities and skills drills.
- Develop a generalized, standardized plan for deploying Django projects.
- Describe each step required to deploy a Django project to a remote server.
- Each experience in class today is engineered to support shipping your Contractor Project!
- In two hours, you'll have two opportunities to deploy a Django site to Heroku, along with two successfully shipped mini projects that you can reference when preparing to deploy your Contractor Project for the first time.
- The step by step guide in this plan works for any Django application deployed to Heroku.
- Wise students may consider bookmarking this plan to ensure stress-free, successful Django deployments in the future. Intensives kick off at the start of the new year!
This guide is resource meant to walk you through deploying ANY Django project on Heroku. Please note the following guidelines to ensure your deployment experience goes smoothly!
- Replace references to
uniqueprojectname
with a unique and memorable name for your project.- Spaces aren't allowed in a project name, so be sure to replace any spaces in the string with dashes (
-
).
- Spaces aren't allowed in a project name, so be sure to replace any spaces in the string with dashes (
- Replace references to
projectname
with the name of your Django project.- Not sure what the project's name is? Locate the folder that contains
settings.py
‒ the name of that folder is your Django project name.
- Not sure what the project's name is? Locate the folder that contains
Now that we know how to use this guide later on, let's get started. Open your your Django tutorial codebase in your editor, and complete each step as guided by the instructor.
In your project root, create a file called Procfile
and add the following line of code inside:
web: gunicorn projectname.wsgi --log-file -
What does this line of code tell Heroku to do?
web
: Informs the Heroku Dyno that your project is a web project.projectname.wsgi
Tells the Dyno to look at a file calledwsgi
in the folder calledprojectname
.- This file is included when you create a new Django project.
—-log-file -
Write all terminal output to the logs.- View your Heroku logs by running
heroku logs
.
- View your Heroku logs by running
Heroku recommends completing this step early on to make sure your Procfile
works, and that the Dyno has everything it needs in order to successfully install your project and deploy it live.
🤔 What files are required to successfully deploy on Heroku?
The following files in this list live in the project's root directory, and must be included in order to successfully deploy your project:
requirements.txt
: A list of dependencies to install before starting the server.Procfile
: Contains the command Heroku will run to start the server.
Add the new dependencies to requirements.txt
so that they are installed when you next push to Heroku:
django==2.2.7
gunicorn
-
3a: In your terminal, run the following command to create a new Dyno, where your project will be installed and deployed for use by the public.
heroku create uniqueprojectname
-
3b: Open your editor, and add the following strings to the blank
ALLOWED_HOSTS
list insettings.py
:ALLOWED_HOSTS = ['localhost', 'uniqueprojectname.herokuapp.com']
🤔 What do these values mean?
localhost
: Development environment ‒ the place where you write code.uniqueprojectname.herokuapp.com
: Production environment - the place you deploy your finished code to show others.
Add the following string to settings.py
:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Add, commit, and push your changes to Heroku!
git add .
git commit -m "tweaked settings for deployment"
git push heroku master
-
⚠️ GET AN ERROR?: When deploying with Git, Heroku wants to initiate the repo from the project root, where yourmanage.py
file lives. You might notice in the tutorial repo thatmanage.py
is one level down. Even though running the app locally with Heroku worked, if you try to deploy withgit push heroku master
you’ll receive an error that saysFailed to detect app
. You can fix this by running the following command instead ofgit push heroku master
:git subtree push --prefix projectname heroku master
-
🛑 GET A DIFFERENT ERROR?: Run
heroku logs
in your terminal, then raise your hand to receive 1 on 1 feedback from your instructor on the state of your deployment. -
🤔 Why does pushing to Heroku take so long?!
- Each time you push to Heroku, it kicks off the following deployment workflow:
- Examine
requirements.txt
for changes since the last deployment.- Installs new dependencies automatically.
- Run all new migrations found since the last deployment.
- Heroku executes the exact same
manage.py
commands we've practiced all term!
- Heroku executes the exact same
- Launch the production server according to the command in the
Procfile
.
- Examine
- Each time you push to Heroku, it kicks off the following deployment workflow:
Once you know everything works, push to GitHub.
git push origin master
So far, we've accomplished:
- Creating a live website, located at
https://uniqueprojectname.herokuapp.com
. - We've done all the work required to release new features and bug fixes early and often!
You should strive to frequently push to your production server ‒ anytime you fix a bug or add a new feature. That means you may find yourself pushing code to Heroku multiple times a day ‒ just like most major companies!
- Follow all the steps above to deploy the MakeWiki project. When complete, you should have a live, working website that looks similar to this example created using today's step by step plan.
- Add at least two new Pages in the admin.
- Test the application's workflow in the browser:
- Sign Up
- Log In
- Create Page
- Log Out
- Let's practice deploying a change to our project.
- In
wiki/views.py
, modifyPageListView
to return allPage
objects sorted by thecreated
date, newest first. - Test the change locally to make sure it works.
- Add, commit, and push the change to Heroku.
- Verify that the change on your live website.
- In
- Add your Heroku link to column K of the Course Tracker.
- (Optional) Celebrate your second deployment! Slack the link to your working deployment in our class channel.
Please fill out the Course Feedback Form by the end of class today.