Author: aashutosh
Updated: 15 Feb 2023 10:50 p.m.
Summary : So, done with your Django app development, now time to deploy your project to Heroku? Follow these easy steps to deploy, host your Django app to Heroku.
2. Clone app to Heroku
a. Login to heroku
This command will open a browser window to login to your heroku credentials
$ heroku login
b. Link your respository to heroku
There are several options,
Create New app from browser or else,
Create New app from command line
$ heroku create <heroku-app-name>
Clone heroku app source code to local
$ heroku git:clone -a <heroku-app-name>
$ cd <heroku-app-name>
Link existing project to heroku app
$ cd <your-project>
$ heroku git:remote -a <heroku-app-name>
3. Activate your virtual environment.
$ source ./envs/bin/activate # - Linux
$ .\env\Scripts\activate # - Windows
4. Create Procfile & requirements.txt
Procfile
Heroku runs Djnago apps via gunicorn web server, recommended for production apps. Procfile declares Djnago apps’s process types and entry points. This file should be present in project's root directory.
$ pip install gunicorn
$ cd <your-project>
$ vi Procfile
web: gunicorn myproject.wsgi
requirements.txt
Heroku recognizes Python & Django apps via reading requirements.txt file which should be present in projects home directory. It contains modules/packages required for running the app. Include all your project dependent packages in requirements.txt
$ pip freeze > requirements.txt
5. Add Static file configuration details in <your-project>/settings.py
#Static Files
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
6. Serve Static files via Whitenoise
Edit <your-project>/settings.py to add whitenoise configuration to server static file to your python/Django Web app.
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
]
7. Initialize git repository in your project
$ cd <your-project>
$ git init .
$ python manage.py collectstatic
$ git add .
$ git commit -m "deploying to heroku first time"
8. Deploy or push codebase to Heroku
$ git push heroku master