![]() |
VOOZH | about |
Heroku is a platform-as-a-service (PaaS) for deploying, managing, and scaling applications. There are two ways to deploy a Django project on Heroku:
This section explains how to deploy a Django project to Heroku using Git CLI, covering both project setup and Heroku configuration.
Before you begin, make sure you have:
Follow the below steps to deploy your Django project to Heroku using Git CLI:
Install the essential libraries needed for deployment:
Run:
pip install gunicorn whitenoise dj-database-url
Note: Additional dependencies may be required depending on your project.manage.py):web: gunicorn <project_name>.wsgi
pip freeze > requirements.txt
Disable Debug Mode
DEBUG = FALSE
Configure Allowed Hosts
ALLOWED_HOSTS = ["<your-app-name>.herokuapp.com"]
Add WhiteNoise middleware
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]
Configure Static Files
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Configure Database (PostgreSQL).
import dj_database_url
DATABASES = {
'default': dj_database_url.config(conn_max_age=600, ssl_require=True)
}
Initialize Git (if not already done):
git init
git add .
git commit -m "Initial commit"
You can then choose:
heroku login
heroku create <your-app-name>
git push heroku main
heroku run python manage.py migrate
heroku open
If needed, run:
heroku run python manage.py collectstatic
heroku logs --tail