Right now, you know how to build a Django API on your local machine. Time to get your work out into the world by deploying it!
By the end of this, developers should be able to:
- Push latest code to Heroku.
- Setup PostgreSQL Database on Heroku.
- Create Procfile for deployment.
In addition to the steps below, feel free to utilize this recorded walk-through of deploying.
-
Passcode: @m9R009E
Follow these steps in the root of a Django API repository.
- If you haven't already, make sure the virtual environment is running with
pipenv shell
. cd
into your project directory for the remainder of the steps below.- We will use a package called
gunicorn
to make our app feel faster and better utilized. Install it withpipenv install gunicorn
. - Create a file called
Procfile
(don't forget the capitalP
!). - Inside the
Procfile
, add the following lines, replacingyourprojectname
with your project's name (the name of your project folder, which containssettings.py
,wsgi.py
, etc. files). The following example assumes the structureyourprojectname/wsgi.py
:web: gunicorn yourprojectname.wsgi
release: python manage.py migrate
- Handle static assets. Many of these will be for our admin views right now
- Run
python3 manage.py collectstatic
inside of your repository. - Install the
whitenoise
package by runningpipenv install whitenoise
. - Follow the steps from the whitenoise quickstart for Django apps section to modify the
settings.py
file in your project. Do not run the install step, as we did that already.
- Run
- As a safety measure, run
pipenv lock
to ensure thePipfile.lock
is up to date. - All of our dependencies across projects are right now stores in one
Pipfile
andPipfile.lock
in thedjango-env
folder. In order to deploy, we need those dependencies in this directory.- Run
pipenv run pip freeze > requirements.txt
inside the root of your repository. This will create a new filerequirements.txt
that Heroku can use to properly initialize your project on deployment.
- Run
- Create heroku app with
heroku apps:create yourappname
replacingyourappname
with the name you want to use for your heroku app. - Add the PostgreSQL add-on with
heroku addons:create heroku-postgresql:hobby-dev
. - Add config keys with
heroku config:set THING=value
. You'll need:- A
SECRET
ex:heroku config:set SECRET='na807h9asgfa7gsdf'
- A
CLIENT_ORIGIN
ex:heroku config:set CLIENT_ORIGIN=https://my-username.github.io
- Note: Make sure there is no trailing slash
/
at the end of theCLIENT_ORIGIN
- Note: Make sure there is no trailing slash
- A
- Run
heroku config
to confirm you have aSECRET
,CLIENT_ORIGIN
, andDATABASE_URL
(for the PostgreSQL addon). - Add, commit, and push to
origin
all changes made from the above steps. - The actual deployment happens when you push your code to the
heroku
remote. Make sure you have all the code you want added and committed, then rungit push heroku main
.
First step upon encountering an issue should be to run heroku logs --tail
to read the logs of your deployed heroku server. Then, you can make an issue if you are still unsure.
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.