Django Settings File Tricks (Posted on March 9th, 2013)

Here are two really awesome tricks that have helped me with keeping my code sane on development and production.

Environment Variables

Environment variables are just like they sound. They are variables local to an environment such as development and production. You can basically store variables on the system so that when Django starts up it can use a certain variable value from development and a different one from the production server. We can set a DEBUG variable like so using a terminal window:

export DEBUG=

And we can read it like so:

import os
ENVIRONMENT = os.environ
DEBUG = bool(ENVIRONMENT.get('DEBUG', True))

We try and get the DEBUG variable from the system environment. If we can get one then we set our Django DEBUG setting to it. If we can't find one then we set it to True by default. So you can set your DEBUG value to False on production and then not set anything in your development environment for it to automatically be set to True. The cast to a bool is needed since the OS will store variable values as a string. Python treats empty strings as False which is why we don't assign a value to our DEBUG variable in the environment. If we wanted to set debug to True on the server then we could set our environment variable as such "export DEBUG=True".

File Path

It is often the case that where you store your files on your development machine is different from where you store your files on your production machine. So rather than storing two different file locations you can have Python get the file location for you.

SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

This will get the folder where your settings file is stored. You can then tell Django where your static files are stored by doing something like:

STATIC_ROOT = os.path.join(SITE_ROOT, 'static')

These two tricks have helped me in the past with going from development to production with ease. If you know any others post them in the comments below. Thanks for reading!

Tags: Django

Comments:

  • Anonymous - 2 years, 2 months ago

    I use environment variables to set DEBUG to True. Much like your case but reversed. Also there is getenv() which I believe is more suitable than directly working with the environ dict. from os import getenv DEBUG = bool(getenv('DEBUG', False)) This way i can use DEBUG=Y ./manage.py ... in development and just ./manage.py ... in production. For absolute paths I like to use a function: from os.path import abspath, dirname, join from os import sep as DIRECTORY_SEPARATOR def path(*args): return join(abspath(dirname(__file__)), *args) + DIRECTORY_SEPARATOR STATIC_ROOT = path('public', 'static') # /path/to/app/public/static/ STATICFILES_DIRS = (path('static'),) # /path/to/app/static/

    reply

  • Nai - 2 years, 2 months ago

    Where do you normally put these environment variables in? My approach is to use different settings file for different environments.

    reply

  • Max Burstein - 2 years, 2 months ago

    You enter them in a terminal window. They're basically stored along with your bash profile. I've updated the post to make that more clear. Sorry for the confusion.

    reply

  • Chris - 2 years, 2 months ago

    > Django DEBUG setting [...] to True by default Debugging may include displaying stack traces or other internals to the user. Its not a good idea to have that as a default. Always use "production" as the default and override it with "development" settings. You may forget to set the DEBUG value on the production server after a new install, or somebody deletes it for whatever unrelated reason.

    reply

  • Anonymous - 1 month, 1 week ago

    I really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot! <a href='http://damdaar.com/health-news/' target='_blank'>health news</a>

    reply

  • cheat pou - 1 month ago

    Excellent and very exciting site. Love to watch. Keep Rocking.

    reply

  • hill climb racing cheat - 1 month ago

    The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface.

    reply

  • spinner - 2 weeks, 5 days ago

    spinner

    reply

  • essay service reviews - 2 weeks, 5 days ago

    So many times we are using these online essay writing books steps. The information is very helpful to our school students. This is the correct way for getting good knowledge and good education.

    reply

  • Telestep ladders - 1 day, 1 hour ago

    In Django there are many things to be learnt. I think it will take too much of time. So I have started my searching to know Django in its deep. I have read the two tricks and I guess that those are very interesting and also very helpful for the users.

    reply