1. Prerequisites 📝

Before you begin, make sure you have the following installed and ready: Python 3.7 and pip on your machine
Git for version control
A free account on Heroku
Django project folder initialized locally
Virtualenv or venv to isolate dependencies

2. Create Heroku Account and Install CLI ⚙️

Sign up at Heroku and install the Heroku CLI: Download installer for your OS from the official site
Run heroku login in your terminal and follow prompts

3. Prepare Your Django Project 🔧

Adjust your project settings so Heroku can serve it seamlessly.

3.1 Update settings.py

• Add ‘whitenoise.middleware.WhiteNoiseMiddleware’ to MIDDLEWARE
• Use django-environ or os.environ to load SECRET_KEY and DEBUG from environment
• Configure ALLOWED_HOSTS to [‘your-app.herokuapp.com’, ‘localhost’]

3.2 requirements.txt, runtime.txt, Procfile

File Purpose Example
requirements.txt List dependencies Django==4.1.0
gunicorn==20.1.0
dj-database-url==0.5.0
whitenoise==6.2.0
runtime.txt Specify Python version python-3.9.13
Procfile Define process types web: gunicorn myproject.wsgi –log-file –

4. Initialize Git Repository and Commit 📦

Heroku deploys via Git. If you haven’t already: Run git init in your project root
Create a .gitignore (exclude venv/, __pycache__)
git add . and git commit -m Initial Django app for Heroku

5. Create Heroku App and Configure Add-ons 🛠️

• Run heroku create your-app-name to provision a new app
• Add Heroku Postgres: heroku addons:create heroku-postgresql:hobby-dev
• Verify config with heroku config

6. Set Environment Variables 🔒

Secure your secrets: heroku config:set SECRET_KEY=your-production-secret
heroku config:set DEBUG=False
For Django-Environ users, set DATABASE_URL automatically by Postgres add-on

7. Deploy to Heroku 🚀

Push your code for the first time: git push heroku main (or git push heroku master)
Heroku will install dependencies, detect Python, build slug
Watch the output for build success ✅

8. Run Migrations and Collect Static 🗄️

heroku run python manage.py migrate to apply database migrations
heroku run python manage.py collectstatic –noinput to gather static files via WhiteNoise

9. Monitor, Scale, and Manage Logs 📊

• Check logs: heroku logs –tail
• Scale dynos: heroku ps:scale web=1
• Inspect Postgres: heroku pg:info

10. Custom Domain and SSL 🌐

If you own a domain: heroku domains:add www.yourdomain.com
Configure your DNS CNAME to the Heroku endpoint
Heroku provides free SSL certificates automatically

Conclusion ✅

You’ve successfully deployed your Django application to Heroku! 🎉
From here, explore performance monitoring, CI/CD integrations, and team collaboration features. Keep your dependencies updated, review your logs regularly, and enjoy the power of seamless cloud deployment.
For more details, visit the Heroku Dev Center and the Django documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *