In Django

Overview

  1. Create Virtual Environment
  2. Initialize Git
  3. Create Project on Heroku With Buildpacks
  4. Run Basic Pip installs
  5. Requirements.txt file
  6. Procfile
  7. runtime.txt
  8. gitignore
  9. Configure WSGI file
  10. Configure Settings.py file
  11. Push to Heroku
1 – Create Virtual Environment

To push and work on a project in heroku we will need to set up and work withing the virtual environment.

In our command promp navigate to the projects root directory and run the following command.

virtualenv lims10env 

The second part of the command needs to be the name of your virtual environment. This can be whatever name you like. I named mine “lims10env” becasause my projects name is “lims10”

Activate You Virtual Environment:

Now that we created our virtual environment we need to activate it. From you projects root directory (Where your virtual environment should now be) activate your virtual envorment by running

lims10env\scripts\activate

2 – Initialize Git

If you already have  a Git account setup run git init from you command prompt to initialize. If you are not already set up you will be prompted to login to your git account

3 – Create Project in Heroku

This can be done directly in Heroku but we will do everything form the command prompt. Each project in heroku will need build packs, wich tell heroku what programming languange we are using. This can also be done in heroku but we will create our project and build packs in one line  from our command prompt.

heroku create lims10 –buildpack heroku/python

“Heroku create “Project name” will create our project while “–buildpack heroku/python” adds the buildpack.

Checking the current get remote

Just to make sure we dont overwrite any of our other heroku project I like to make sure my get remote is connected to the right project. To do this we can run:

git remote -v

Changing the current git remote

You should see the url to you new project. If the get remote is to another project you can change it by running:

heroku:git remote -a you-app-name

4 – Pip install dependencies

When we move our project to a server all the dependencies our project relied will no longer work. We need to make sure these dependencies are installed in our virtual environment so all our projects features will work.

Lets Install the minimum dependencies our project needs. From your virtial environment run these commands one by one:

  • pip install django
  • pip install Whitenoise
  • pip install Psycopg2
  • pip install Gitignore
  • pip install Dj-database-url
  • pip install Dj-static
  • pip install Static3
  • pip install Gunicorn

Now install any other dependencies that you may have that are uniwe to your project.

5 – requirments.txt

No that we have install our dependencies we need to create a requirments.txt file so heroku can find this. To do this run:

pip freeze > requirements.txt

This will create a file call “requirements.txt” in our projects with a list of dependencies. Every time we add new dependencies we need to run this command and the dependency will be added to the file

6 – Procfile

Heroku apps need something called a “Procfile”. This is a simplified text file with no extension. So in our projects root directory create a file called “Procfile”. Ensure that the “P” is capitalized.

At this point wouldnt be able to tell you much about the file other that it helps connect to heroku web dynos and is required. In the file include the following text (Replace “appname” with you projects name).

Thats all that goes into the Procfile for now.

7 – runtime.txt

Our runtime.txt file takes only one line and needs the version of python our project runs on. Make this file the root directory and add the following code (Replace python version with version you are using)

8 – gitignore

We need to add a “gitignore” for any aspects of the project that we use for our local version but may interfere with a live project.

Create the file in your projects root directory and make sure the file name starts with a dot “.gitignore”

So far here are the only files we want to ignore. I ignore sqlite because i dont want it to interfere with my live database and dont want my virtual environment in production.

9 – Configuring WSGI File

We have just a few more steps before we push to heroku. We need to edit our WSGI file to look like this:

10 – Configuring Settings.py

The final file we need to configure before we push our project live is settings.py.

At the bottom of settings.py add live version for your project

11 – Pushing To Heroku

git add .

git status

git commit -m “Leave a comment”

git push heroku master

When you make your first push you may recieve the following error after running “git push heroku master”.

If you get this run the following commande and then try pushing again. This should take care of the issue.

heroku config:set DISABLE_COLLECTSTATIC=1

If you happen to run into this error

Run “heroku logs” in the command prompt and you should see a issue description.

One issue i recently ran into was with white noise. There may be a new way to implement white nise but for now i just reverted back to whitenoise==3.3.1, run pip freeze and push live again.

Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.