First thing you need to do is to create an account. Heroku offers four pricing plans: Free, Hobby, Standard, Performance. In this tutorial we are going to use the Free-plan. It’s necessary to remember that this plan has certain limitations you need to peruse with before you deploy django app to heroku.
If you already have an account on Heroku, skip this step. To create an account you need to fill out a registration form by clicking on the https://signup.heroku.com/identity link. It shouldn’t take you more than one minute neither bring any difficulties. The only thing I want to pay attention to is that don’t forget to choose Python at “Primary Development Language” line. After filling the form confirm your email and let’s get down to business.
We’ll need Heroku CLI. In this tutorial we will quite often use CLI to interact with Heroku.
After the installation process is finished, open console and enter the username and password of your account.
For this article I wrote a simple Django application. First, we need to do some adjustments to our app before starting to deploy django to heroku.
requirements.txt is a file which contains a list of all necessary app packages. Create this file in the root folder with such content inside:
Procfile describes command need to be executed for app launch.
It is the file in which we specify Python version
We need to get three files with the following settings:
Such separation of settings will become necessary when our app starts to grow. You will find link to the code of these files at the end of this article.
For the correct display of static files we need to modify settings.py и wsgi.py:
In the previous step we’ve created PostgreSQL database. Let’s connect it to our app. Go to “Settings” and copy the entire DATABASE_URL contents.
After that, paste contents into settings/production.py., you must end up with this:
Preliminary configuration of our app is done. Before we start to push django on heroku, let’s check how our app is displayed locally. For this we have heroku local web command.
If no mistakes were returned and you don’t see any of them in the console, then everything went well and you can proceed to the next step.
Logging into Heroku account
We'll push code and heroku should deploy the app automatically.
Don’t forget to add --settings=heroku_blog.settings.production, because that’s how system understands that particular file with production-server settings has to be used.
After completing all the steps above we get ready-to-go app which can be found at https://django-heroku-blog.herokuapp.com/
If you’re receiving errors while deploying an app, you can check what went wrong
Add two new packages:
Take REDIS_URL value from Settings and paste it to settings/base.py. After that, the Celery settings should look like that:
For Celery celery.py file has to be created in which celery initialization will take place. More details on how to set up celery in Django you can find here Using Celery with Django.
Let’s add celery test-task. This task was taken from the documentation and what it simply does is it returns a sum of two numbers.
As we have seen before, Procfile contains commands that need to be executed if we want to launch an app. We need to launch celery at the moment of the app launch. To do this we need to update procfile by adding the string worker: celery worker -A heroku_blog -E -l info. As a result, procfile should look like that:
We’re all set! Now let’s double-check if we did all correctly:
Open console and run a command
In the next tab run log output, there should appear statement that the celery task was completed successfully.
That’s it. The app’s source code you can find here: https://github.com/barbossa/django-heroku-blog.