Django: Installing Heroku

Django Heroku San Francisco

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.

Installing Heroku 

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:

$ Heroku login

Enter your credentials for Heroku; the program will hide your password as you type it in.

Preparing Heroku

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:

web: gunicorn filename.wsgi --log-file –

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:

pip freeze > requirements.txt

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:

Django==1.9.8
dj-database-url==0.3.0
dj-static==0.0.6
gunicorn==19.6.0
Unipath==1.0
python-decouple==3
Pillow==3.3.0
Markdown==2.6.6
bleach==1.4.3
psycopg2==2.6.1
whitenoise==3.2

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:

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

Now, install your whitenoise for the static file and update your requirements.txt file. Here is how:

pip install whitenoise

Once whitenoise is installed, add it to the Django Heroku application in your wsgi.py file:

import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "bootcamp.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
Finally, you can update settings.py using this code:
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

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.

Deploying Heroku 

Additional lessons from a data class might also cover how to deploy Heroku. First, make a clone of your repository using this command:

git clone https://github.com/vitorfs/bootcamp.git && cd bootcamp

Now, log in to Heroku running this command:

Heroku login

Create your Heroku App in the root of the project using this code:

Heroku create demo-bootcamp

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:

Heroku addons:create Heroku-postgresql:hobby-dev

This is what the output will look like:

Creating postgresql-metric-21979... done, (free)
Adding postgresql-metric-21979 to demo-bootcamp... done
Setting DATABASE_URL and restarting demo-bootcamp... done, v4
Database has been created and is available

Access the app by logging into the Heroku Dashboard:

Heroku demo-bootcamp Application

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:

settings.py
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', default=False, cast=bool)
DATABASES = {
    'default': dj_database_url.config(
        default=config('DATABASE_URL')
    )
}

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:

Counting objects: 1999, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (616/616), done.
Writing objects: 100% (1999/1999), 461.28 KiB | 0 bytes/s, done.
Total 1999 (delta 1139), reused 1983 (delta 1131)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-2.7.12
remote:      $ pip install -r requirements.txt
...
remote: -----> Launching...
remote:        Released v5
remote:        https://demo-bootcamp.Herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy.... done.
To https://git.Heroku.com/demo-bootcamp.git
 * [new branch]      master -> master

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.

Closing Thoughts 

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.

*Please note, these articles are for educational purposes and the topics covered may not be representative of the curriculum covered in our boot camp. Explore our curriculum to see what you’ll learn in our program.

Get Program Info

The following requires your attention:
Back
Back
Back
Back
Back
Back
Back
Back
Back
0%

Ready to learn more about Berkeley Data Analytics Boot Camp in San Francisco? Contact an admissions advisor at (510) 306-1218.