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


  • Anonymous - 2 years, 3 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 ./ ... in development and just ./ ... 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/


  • Nai - 2 years, 3 months ago

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


  • Max Burstein - 2 years, 3 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.


  • Chris - 2 years, 3 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.


  • Anonymous - 2 months, 3 weeks 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='' target='_blank'>health news</a>


  • cheat pou - 2 months, 1 week ago

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


  • hill climb racing cheat - 2 months, 1 week ago

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


  • spinner - 1 month, 4 weeks ago



  • essay service reviews - 1 month, 4 weeks 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.


  • Telestep ladders - 1 month, 1 week 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.


  • visitwebsite - 6 days, 16 hours ago

    This blog is so nice to me. I will keep on coming here again and again. Visit my link as well..


  • getting instagram followers - 5 days, 19 hours ago

    Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me.


  • 100 comments - 5 days, 19 hours ago

    Your blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging..


  • voyance directe - 5 days, 4 hours ago

    This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post!


  • free instagram followers - 2 days, 15 hours ago

    I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well..


  • free likes on instagram - 2 days, 15 hours ago

    I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post.


  • Manual Testing Training in Chennai - 2 days, 11 hours ago

    Thanks for the article on Django Settings .Very informative one.