The past few days have been an enormous PAIN thanks to trying to get a Django application up and running on the internet. I am glad to say that said PAIN is no more. 😀
I first started with Digital Ocean, because I had used it before to deploy a project I had not even worked on, but I found a ‘how to’ guide online that made the process fairly simple. From this bearable experience, I thought to myself, why not use Digital Ocean for deployment *big mistake*. Long story short, I lost a week of my life.
Enough lamenting. Now onto the reason of this article.
For developers looking for a straight forward and easy way to deploy an application, Heroku is an advisable option. It was made for developers to easily deploy their apps. *I’m drawn to convenience*
Here is how I did it:
Once HerokuCli is installed, you can use the `heroku` command from your command line.
Create a heroku account
If you don’t already have one, sign up to heroku, cause err, you kinda need it. It’s free, just in case you are wondering.
Login to heroku
Login to heroku from your command line.
This command opens your web browser to the Heroku login page. If your browser is already logged in to Heroku, simply click the Log in button displayed on the page.
Once logged in, your session is already save and you don’t have to keep logging in.
I’m assuming you already have your django application on your local computer by this step.
I had no idea what Gunicorn was and what it did. I just took it as a given and hoped my application would work. Gunicorn, (Green Unicorn) is a web server for python applications. A web server is what takes requests from the browser, sends the request to the Django app and then forwards the response from Django, back to the browser.
Run the command in the root of your project folder:
pip install gunicorn
The Django-Heroku package automatically configures your Django application to work on Heroku.
pip install django-heroku
Create a requirements.txt file
A requirements.txt file is a file that contains a list of all the libraries required for your application to work. It lists everything installed with `pip install`.
PS: Remember to have your virtual environment activated for this.
pip freeze > requirements.txt
The requirements.txt file should look something like this:
Create a heroku app
Create a heroku application if you don’t have one already. You can do this through the command:
or create one from your heroku dashboard. I prefer the latter because I can name the app whatever I want whilst creating it.
Create a postgres database for your application
For django applications, I believe postgres is a good enough database. It’s all I have used anyway:)
heroku addons:create heroku-postgresql:hobby-dev
hobby-dev is a plan tier that tolerates up to 4 hours of monthly downtime. For more on postgres tiers.
Create a Procfile in the project root
The Procfile is what tells heroku how to run your application.
If you have gunicorn installed (which you should), then set this line in your Procfile.
web: gunicorn myproject.wsgi:application
my project is the name of your django application.
PS: Remember to maintain case sensitivity when naming the file, i.e keep it as Procfile, and not ProcFile. I named mine ProcFile and my heroku application could not read it. Linux is case sensitive so the two are considered different files.
Import dependencies from requirements.txt to a Pipfile
Heroku installs dependencies from a Pipfile or a requirments.txt file. I chose to migrate because my Pipfile was not up to date. However, just the requirements.txt file can be used to install dependencies.
But if you have both the requirements.txt file and Pipfile, heroku uses the Pipfile.
Run this command to import the dependencies in your requirements.txt file to a Pipfile
pipenv install -r path/to/requirements.txt
Commit application code to heroku
We’re almost at the end of the tunnel.
Initialize a git repository in your project root.
cd <project> git init
Link your folder with your heroku app.
git remote add heroku <heroku git url>
To find this URL, login in to your heroku dashboard -> apps -> your-app-name -> settings -> info
Deploy your project
git add . git commit -m 'message' git push heroku master
Migrate your db to the heroku app
heroku run python manage.py migrate