Learn Django

Define Environments

When working on software projects it is common to have the application code, or variations of it, running in different environments. There are commonly three:

Development
The development environment is your local computer. All the work you've done so far was just to get your development environment setup. This environment is where you work on building and fixing code.

Staging
A staging environment is a place to deploy code before it goes into production. It exists because it's usually a good idea to push your code to an environment that mimics production and test it before you make the changes on the site that end users actually use.

Production
This is the live version of the application - the version that stores real data, and serves real customers. Changes to production should be thoroughly tested so that you don't compromise the end user experience or data.

Why Is this Important?

Django treats development and production differently in a few critical ways. One is how it reports errors. In development, if Django encounters an error it can show an error page with not only the error, but your complete settings file. This file contains information you want to keep secret. It's therefore critical that this page isn't shown to any users if they encounter an error on your site.

How to Differentiate Between the Two Environments

There are several different ways to differentiate between the two. The one chosen in this course is to use Environment Variables. The steps that follow will show you how to set the proper Django and Environment Variables settings in order to properly configure development and staging environments.

Step 1: Importing Environment Variables

Now that the project relies on importing environment variables to work properly, it's important to have the right error checking in place in case a variable isn't set. Python's default error messages for importing environment variables isn't very helpful. To make up for it we'll equip the project with a helpful import tool that will provide the right error message if a variable doesn't exist.

Credit for this code goes to the awesome e-book, Two Scoops of Django. I highly recommend this book as the recipes you learn in it will be useful on your journey as a Django developer.

Open the /.../crmeasy/crmapp/settings.py file in your IDE. Locate the following two lines of code (near the top) and delete them:

1
2
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

Then add these lines of code in their place:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# add this to the import section of the file
import os
from django.core.exceptions import ImproperlyConfigured

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

# Handling Key Import Errors
def get_env_variable(var_name):
    """ Get the environment variable or return exception """
    try:
        return os.environ[var_name]
    except KeyError:
        error_msg = "Set the %s environment variable" % var_name
        raise ImproperlyConfigured(error_msg)

# Get ENV VARIABLES key
ENV_ROLE = get_env_variable('ENV_ROLE')

Step 2: Set Environment Role Variable

The first variable to set is the one that tells Django what environment it is running in. This is achieved by setting the ENV_ROLE variable to either 'development', 'staging', or 'production'. Follow these steps to set the variable to 'development' on your local computer.

On a Mac

How to set the variable on a Mac.

1
$ vi ~/.bash_profile

Now the .bash_profile file will open for you to edit. Follow these steps to add a line.

  1. Press the i key. This will allow you to edit the file.
  2. Use your down arrow key to navigate to the end of the file. Any line that starts with a ~ is not an actual line in the file; it's a non-line marker.
  3. On the last line add export ENV_ROLE=development to the file.
  4. Press the esc key
  5. Press the keys :wq in that sequence, then press the enter key.
  6. Close your terminal and re-open it.

On Windows

How to set the variable on Windows.

  1. Open a command prompt
  2. Type setx ENV_ROLE development

Step 3: Setting the DEBUG Value

The DEBUG value will determine how Django displays errors. If the value is set to True, a debug page will be shown when errors are encountered. As mentioned before that's good for the development environment but very harmful for production. The below code is how your DEBUG and TEMPLATE_DEBUG settings should be set.

With /.../crmeasy/crmapp/settings.py open, locate the DEBUG and TEMPLATE_DEBUG settings. Delete them both. Then add this code. Use the #SECURITY WARNING: don't run with debug turned on in production! as a marker.

1
2
3
4
5
6
#SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
TEMPLATE_DEBUG = DEBUG
if ENV_ROLE == 'development':
    DEBUG = True
    TEMPLATE_DEBUG = DEBUG

Step 4: Commit Changes

Execute these commands to commit your changes in Git.

1
2
3
4
5
# add files
(venv)$ git add .

# commit files
(venv)$ git commit -m "created dev env, set debug values"

Track your progress with a free account