How to Deploy Django Application to Heroku
Hello, my name is Erwin and now we talk about how to deploy Django application to hosting site named Heroku. Why heroku ? because heroku provide free plan to host our application on the internet and it’s also simple and easy for anyone especially first timer to deploy their apps.
Django is a powerful Python web framework hat encourages rapid development and clean, pragmatic design. Beside that Django also has many technical features such as own web server, MVC architecture, HTTP libraries, and many more
In this blog, We will discuss how to deploy our Django app to heroku especially on Windows.
Preparation
- Create Heroku account
- Install Heroku CLI and git
- Install Project Requirements
- Create Heroku App and Deploy
Create Heroku Account
- Open Heroku web https://www.heroku.com
2. Click Sign Up button on top right
3. Fill account information below with valid email and press Create Free Account. You will receive your activation email shortly
4. Open your registered email. If heroku mail didn’t show up, wait for a while. Then press link on that email (in photos below is blocked)
5. Then login into your heroku account
Install Heroku CLI
- Go to Heroku CLI web page here
- Choose your installation method based on OS
Install Git
- Open git scm website here, then choose installer based on your OS
Install Project Requirements
- Add a Procfile into your project root directory. You can use this command on your terminal
echo “web: gunicorn YOUR_PROJECT_NAME.wsgi —-log-file -” > Procfile
2. Install some required packages to make application can run in Heroku server:
- gunicorn ( app server )
- dj-database-url ( for database )
- whitenoise (for serving static files)
- psycopg2 (postgres database)
You can install them with this command on your terminal:
pip install gunicorn dj-database-url whitenoise psycopg2
3. Add all your project requirements to your requirements.txt :
pip freeze > requirements.txt
4. Add runtime.txt file to your root directory and add your python version, you can check on your terminal. For example
python --version
python 3.9.0
Then, add into runtime.txt
echo "python-3.9.0" > runtime.txt*Note: don't forget dash between python and its version
5. You have to add several configuration to your settings.py :
- Set up static assets, preferably add this line at the bottom of the file
...STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
Heroku recommends the use of WhiteNoise (a Django package) to serve static files in production, since Django does not support serving static files in production, by default.
- Add whitenoise middleware at the top of middleware lists in settings.py :
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.security.SecurityMiddleware',
'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',
]
- Update DB configuration in settings.py, preferably at the bottom of the file :
import dj_database_url
django_db = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(django_db)
- Add your app domain name to ALLOWED_HOSTS in settings.py:
ALLOWED_HOSTS = ['YOUR_APP_DOMAIN.herokuapp.com']
Deploy to Heroku
- Now open terminal and log into your Heroku account to start deploy.
PS D:\django_project\djangoapp> heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/4732o90...
Logging in... done
Logged in as john.doe@gmail.com
2. Then create New Heroku app, you can run this command below :
PS D:\django_project\djangoapp> heroku create YOUR_SITE_NAME
3. Next, commit your and push your project to deploy your app.
git init
git add .
git commit -m "Initial Commit"
git push heroku master
On side note, you can set your git remote heroku to your project with this command on your terminal
heroku git:remote -a YOUR_SITE_NAME
4. After that you can migrate the database :
heroku run python manage.py migrate
That’s All
Congratulations, now your heroku app is ready and accessible. Thanks for reading and have a good day !