Deploy Django app to Heroku using this app. This is a step by step guide
- Python 3.6+ Download
- Heroku Account Signup
- A working django Project. If you don't have one you can clone this
- virtualenv or an alternative
- Open your django project as a project in your Idea. I will be referring to pycharm in this tutorial. I will be referring to get-started-with-django Django project named mysite.
- Set up virtualenv. I will be referring to
venv
as the name of my environment. You can easily just add interpreter in pycharm.virtualenv venv venv\Scripts\activate # for windows users source venv\bin\activate # for linux users
- run the server to make sure the application works fine.
python manage.py runserver
- create a
requirements.txt
file. Add the contents of these file requirements.txt. - create a
runtime.txt
file from runtime.txt ref{2-3}. - install the requirements.
pip install -r requirements.txt
- create a file named
.env
and add the contents in .env - create a Procfile like Procfile the following at the top. ref{4}
- Add the following at the top. ref{4}
import django_heroku from decouple import config
- Add the following at the bottom. ref{1,5}
STATIC_ROOT = Path.joinpath(BASE_DIR, 'staticfiles') STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' django_heroku.settings(locals())
- edit your project settings file and update the
SECRET_KEY
,DEBUG
andALLOWED_HOSTS
as configured in settings.# SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = config('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = config('DEBUG', cast=bool) ALLOWED_HOSTS = [] ALLOWED_HOSTS.extend(config('ALLOWED_HOSTS').split(','))
- Update the middlewares. ref{1,5}
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]
- set up git branches. NB: Make sure you are using a repository that you set up so you can have push access.
git add . git commit -m "setting up main branch" git push git checkout -b dev main git add . git commit -m "setting up dev branch" git push origin dev git checkout -b indev dev git add . git commit -m "setting up indev branch" git push -u origin indev # this will set the remote to indev instead of main
- login to your heroku account. login
- Click on new to create new pipeline.
- Name the pipeline and connect the account to github and choose the repository
- Click add new app and create a new app in the staging
- Click add new app and create a new app in the production
- Click the app and visit resource tab and search for Heroku Postgres and apply the Hobby Dev Free.
- Visit the deploy tab and set teh automatic deploy to their respective branch
- Visit the settings tab and add same things in the .env in the config vars. The
ALLOWED_HOSTS
must be your heroku domain name. - Make a Pull request to dev and master to see the update on heroku. Or just deploy directly on heroku.
- Visit the heroku console via the more button and run the following commands one after the other
python manage.py migrate python manage.py createsuperuser