Python Heroku Deployment
Steps to create a postgres database and deply a Python app to Heroku
Install Postgres dependencies
pip install psycopg2
Install guinicorn locally
pip install gunicorn
Install Heroku CLI
https://devcenter.heroku.com/articles/heroku-cli
Login via CLI
heroku login
Create app
heroku create appname
Create database
heroku addons:create heroku-postgresql:hobby-dev --app appname
or you can create from dashboard and add the details to settings.py
Get URI
heroku config --app appname
# Add to your app
Create Procfile at the root
touch Procfile
# Add this
web: gunicorn project_name.wsgi
or
echo "web: gunicorn mysite.wsgi --log-file -" > Procfile
Create requirements.txt
pip freeze > requirements.txt
Create runtime.txt
touch runtime.txt
# Add this
python-3.9.1
Add your hostname to the list of allowed hosts in settings.py
ALLOWED_HOSTS = ['your_app_name.herokuapp.com']
Add STATIC_ROOT
in settings.py
STATIC_URL = '/static/'
# static root
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
Add the static urls in urls.py of the project
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Collect Django static assets
python manage.py collectstatic
put the static folder at the root of your project
Deploy with Git
git init
git add . && git commit -m 'Deploy'
heroku git:remote -a appname
git push heroku master
Add table to remote database
heroku run bash
python manage.py migrate
python manage.py createsuperuser
See heroku logs
heroku logs --tail
Visit app
heroku open