DEV Community

Cover image for Deploy Django Project on Heroku Using Heroku CLI
Manar Abdelkarim
Manar Abdelkarim

Posted on

Deploy Django Project on Heroku Using Heroku CLI

Hello World..

Every time I deploy my project on Heroku I make mistakes and forgot some steps. But not anymore, because today I decided to record them here for you and me.
I will include everything I know that could cause an error or a fail in deployment.

so let's get started

prepare and check your files:

  • STATICFILES_DIRS

not having STATICFILES_DIRS in the settings of your project will cause a fail in deployment so make sure to add this line

STATICFILES_DIRS =[
os.path.join(BASE_DIR, 'static')
]

  • Procfile

you have to add Procfile on the root directory and at the same level of your project and app/s to explicitly declare what command should be executed to start your app, add this line in the file

web: gunicorn myproject.wsgi

and make sure to replace "myproject" with your project name and to install gunicorn , you can install it in many ways such as pip install gunicorn or poetry add gunicorn "if you are using poetry"

  • create requirements.txt file

Heroku knows what libraries to install in its server by reading the requirements file so make sure to create one.
here is a one-line you can type in the command-line to copy the libraries and their version to the file if you are using poetry

poetry export -f requirements.txt --output requirements.txt --without-hashes
Enter fullscreen mode Exit fullscreen mode

or this command if you are using pip :

 pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode
  • check the allowed hosts

if your allowed host list in your .env file and/or the settings are for only the localhost then that will cause a problem. So you can add the URL of your Heroku app to the list such as

ALLOWED_HOSTS = ['mydjangoapp.herokuapp.com']

you can allow all hosts by using [*] then you are fine, and you can change it later after creating your Heroku app and have an actual URL link

  • make sure that everything is working just fine in your local

now we are ready to start, be sure you are standing in the right directory in your command line .. so here is the steps "commands" according to Heroku's official website:

  1. heroku login
  2. heroku create mydjangoapp --buildpack heroku/python
  3. git init
  4. heroku git:remote -a mydjangoapp
  5. git add .
  6. git commit -m "My first commit"
  7. git push heroku main

if your deployment was a success then congratulations on everything you deployed your app successfully.

if you have an error then these are some suggestions :

  • If you get an error message with collectstatic write this command :
heroku config:set DISABLE_COLLECTSTATIC=1
Enter fullscreen mode Exit fullscreen mode
  • install whitenoise and psycopg2 + add whitenoise on the top of the middlewares list in the project settings

'whitenoise.middleware.WhiteNoiseMiddleware'

now let's talk about having a database and you want to migrate it
1- we need to add PostgreSQL to our Heruko project through CLi

heroku addons:create heroku-postgresql:hobby-dev -a mydjangoapp

Enter fullscreen mode Exit fullscreen mode

don't forget to see the PostgreSQL info and add them to the config vars
you can use this command :

heroku config:get DATABASE_URL -a <your_heroku_app_name>
Enter fullscreen mode Exit fullscreen mode

the order of the info that you will get from the URL is like this:
postgres://<username>:<password>@<hostname/server>/<databasename>

next, here is the command to copy the .env vars to Heroku config vars :

heroku config:set $(cat .env | sed '/^$/d; /#[[:print:]]*$/d')
Enter fullscreen mode Exit fullscreen mode

2- let's make migrations

heroku run python manage.py makemigrations
Enter fullscreen mode Exit fullscreen mode

3- migrate to your Heroku database

heroku run python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

4- you can also create a super user

heroku run python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

and here we reach the end .. have a good deploying 😊

References:

https://devcenter.heroku.com/articles/git

Discussion (0)