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:
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.
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.
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.
/.../crmeasy/crmapp/settings.py file in your IDE. Locate the following two lines of code (near the top) and delete them:
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.
$ vi ~/.bash_profile
Now the .bash_profile file will open for you to edit. Follow these steps to add a line.
- Press the
ikey. This will allow you to edit the file.
- 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.
- On the last line add
export ENV_ROLE=developmentto the file.
- Press the
- Press the keys
:wqin that sequence, then press the enter key.
- Close your terminal and re-open it.
How to set the variable on Windows.
- Open a command prompt
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.
/.../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"