Heroku is a Platform-as-a-Service, or Paas. Users subscribe to the service and get a platform to manage, develop and run a variety of applications. If you’re considering attending a data analytics class in San Francisco or elsewhere, you’ll likely learn that Django is easy to use on Heroku since the platform supports most programming languages.
First, sign up on the Heroku website, then install the Heroku Toolbelt, which will help you manage your applications. Next, open up your terminal and log in to the account:
Enter your credentials for Heroku; the program will hide your password as you type it in.
Find an open-source project you want to contribute to on GitHub, and follow along with this Heroku tutorial while you work with Django and Heroku. Keep in mind, you will want to deploy Heroku through the Git software to help you track the changes you’re making.
While you are making changes, a Git repository will store your project. Here are some more things you’ll want to do:
- Add a Procfile to the project root.
- Add a requirements.txt file that has the project requirements in your project’s root.
- Add Gunicorn to your requirements.txt.
- Add a runtime.txt to mark the right version of Python in your project’s root.
- Configure whitenoise for static files.
Creating Your Files
Go to the project root and create a file called Procfile. Add this content to it:
Replace “filename” with your project’s name.
Next, create requirements.txt, which can be done easily if you have a virtual environment and pip. Run this command to create the file:
If you do not have a virtual environment, list the various dependencies to create requirements.txt yourself. Here is an example of what it would look like depending on the versions of your programs:
Now, create a runtime.txt file in the root of your project to add the specific version of Python you use.
Next, set up your static assets. First, set up your parameters for STATIC in settings.py:
Now, install your whitenoise for the static file and update your requirements.txt file. Here is how:
Once whitenoise is installed, add it to the Django Heroku application in your wsgi.py file:
Two libraries can be very helpful when you are deploying Heroku: python-decouple and dj-database-url. python-decouple can separate the source code’s settings parameters by going to the virtual environment’s file. dj-database-url is a simple tool that lets you use a variable from the DATABASE_URL environment when configuring Django Heroku.
Additional lessons from a data class might also cover how to deploy Heroku. First, make a clone of your repository using this command:
Now, log in to Heroku running this command:
Create your Heroku App in the root of the project using this code:
In the code above, “demo-bootcamp” is the parameter for the name of your app. If you do not include a name, then Heroku will choose one. Next, create a database for PostgreSQL in your app:
This is what the output will look like:
Access the app by logging into the Heroku Dashboard:
Go under settings to find the Reveal Config Vars button. Then, add the variables to your virtual environment with some help from the dj-database-url and Python-decouple libraries. Add these variables to your project:
In the code above, notice how the variable DEBUG is false by default. Heroku automatically adds DATABASE_URL after you have installed PostgreSQL. All you need to do now is add your SECRET_KEY. Click “git push Heroku master” to deploy the key. If you have followed these steps correctly, the output should look something like this:
Cost of Using Heroku
Because Heroku is aimed at professionals, it is more expensive than other options. Keep in mind that the free version of Heroku has some limitations as well, like going to sleep after a half hour of inactivity. If you’re just starting out, you might want to look for more inexpensive alternatives like the free version of PostgreSQL.
Additionally, you may want to store media or files uploaded by users; Amazon S3 is a common option used by data analysts.
Heroku is an excellent service for all types of coding. If you are taking a data class, you will definitely want to know how to use the platform.