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, 10 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

  • Life Insurance Calgary - 4 months, 2 weeks ago

    Hi there fantastic blog! Does running a blog such as this take a large amount of work? I've very little knowledge of coding but I was hoping to start my own blog in the near future. Anyway, if you have any recommendations or techniques for new blog owners please share. I understand this is off topic nevertheless I simply needed to ask. Thank you!

    reply

  • instagram followers ! buy instagram likes - 1 week, 1 day ago

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

    reply

  • Nai - 2 years, 10 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, 10 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, 10 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 - 9 months, 4 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='http://damdaar.com/health-news/' target='_blank'>health news</a>

    reply

  • cheat pou - 9 months, 2 weeks ago

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

    reply

  • hill climb racing cheat - 9 months, 2 weeks 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 - 9 months ago

    spinner

    reply

  • essay service reviews - 9 months 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 - 8 months, 2 weeks 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

  • visitwebsite - 7 months, 1 week ago

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

    reply

  • getting instagram followers - 7 months, 1 week 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.

    reply

  • 100 comments - 7 months, 1 week 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..

    reply

  • voyance directe - 7 months, 1 week 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!

    reply

  • free instagram followers - 7 months, 1 week 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..

    reply

  • free likes on instagram - 7 months, 1 week 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.

    reply

  • Manual Testing Training in Chennai - 7 months, 1 week ago

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

    reply

  • Credit Counseling - 7 months ago

    If you're struggling to pay for your financial obligations, bills along with other necessary expenses every month, you likely have considered embracing a with regard to profit credit score counseling support.

    reply

  • Business Insurance - 7 months ago

    Commercial company insurance is really a must-have for small company entrepreneurs. Never dedicate the error by convinced that just since you are running a small company means that the risks tend to be lower and you will go with no insurance protection in order to save on price. Wrong!

    reply

  • Business Loan - 7 months ago

    But for all those people who don't desire to be limited within their lives and prefer to take dangers in existence, business may be the prefect move to make. If you're looking for money with this, business loans would have been a sure assist in this scenario.

    reply

  • Business Technology - 7 months ago

    Business Technology describes elements which facilitate simple business procedures, cut guide labor as well as enable automatic functioning. Today it is an additional name with regard to business technology that's because it contributes towards the needs of each and every business kind and ameliorates overall performance.

    reply

  • Business Insurance - 7 months ago

    Alabama Company insurance products really are a requirement for those businesses big and little. There tend to be two basic kinds of Alabama Company Insurance Needs.

    reply

  • accounting assignment help - 6 months, 4 weeks ago

    All students are following your article because you are providing us a lot of information accounting assignment helps good I am feeling very happy to know this. Thank you very much keep it up.

    reply

  • how to get free instagram followers - 6 months, 3 weeks ago

    Wow what a Great Information about World Day its very nice informative post. thanks for the post.

    reply

  • get free instagram likes - 6 months, 3 weeks ago

    I love this blog!! The flash up the top is awesome!!

    reply

  • buy facebook likes - 6 months, 2 weeks ago

    I like the blog. Awesome present. It is extremely best, of us have got to discover ways to appreciate well before they'll appreciate. lol i realize who appears to be fascinating yet the preferred suggested...

    reply

  • buy twitter followers - 6 months, 2 weeks ago

    I prefer your website. Great write-up. It is extremely authentic, people need to work out how to learn before they could learn. lol my partner and i understand the theory seems intriguing even so their particular best shown...

    reply

  • followers on twitter - 6 months, 1 week ago

    I was looking at some of your posts on this website and I conceive this web site is really instructive! Keep putting up..

    reply

  • how to get more likes on facebook page - 6 months, 1 week ago

    Great things you’ve always shared with us. Just keep writing this kind of posts.The time which was wasted in traveling for tuition now it can be used for studies.Thanks

    reply

  • dermal fillers London - 6 months, 2 weeks ago

    I quite like your blog. Amazing post. It is quite right, persons should learn how to recognize ahead of they can recognize. lol i am aware that will shows up funny the ideal revealed...

    reply

  • facebook - 6 months, 2 weeks ago

    this can be awesome plus meanful. this can be interesting site. Leading is rather handy element. you may have seriously made it simpler for many people who seem to take a look at site and give these folks usefull information and facts.

    reply

  • FrankieRef - 6 months, 2 weeks ago

    <a href="http://viagraonlinebuying.net/">free viagra samples before buying</a> <a href="http://viagraonlineusa.net/">viagra online</a> <a href="http://wheretobuyviagra.net/">buy viagra paypal accepted</a> <a href="http://genericviagraonlinepharmacy.net/">generic viagra 100mg next day</a> <a href="http://canadianpharmacyviagra.info/">canadian viagra without prescription</a> <a href="http://treatmentfored.net/">ed drugs online</a> <a href="http://erectiledysfunctionmeds.net/">erectiledysfunctionmeds.net</a>

    reply

  • green smart living - 6 months, 2 weeks ago

    Such intelligent work on the subject and ideal way of writing here. I am really impressed! This post is a helpful overview of the particular topic and very actionable. Interesting approach!

    reply

  • Fort Lauderdale criminal attorney - 6 months, 2 weeks ago

    May possibly fairly recently up and running an important web log, the data one offer you on this web site contains given a hand to all of us substantially. Bless you designed for your current precious time & get the job done.

    reply

  • Online Cash Loan - 6 months, 1 week ago

    You cannot think about having anything until as well as unless you don't have the needed money. So for that required money you'll want an earnings. However, sometimes the actual salary as well gets completed much prior to the next pay day and below such circumstances the internet Cash Mortgage <b><a href="http://www.vipcarfinance.net/category/online-cash-loan">Online Cash Loan</a></b>

    reply

  • learn about debt management - 6 months, 1 week ago

    a extremely superb webpage. a realy educational in addition to a an extremely superior position. i’m a sucker for the.

    reply

  • catering - 6 months, 1 week ago

    Excellent Submit, I will be a huge believer inside submitting feedback about web sites to be able to allow the website copy writers understand that they’ve extra one thing useful to be able to the net!

    reply

  • click here - 6 months, 1 week ago

    This particular is a superb publish We observed because of reveal this. It really is exactly what I needed to determine wish within long term you'll carry on with regard to discussing this type of superb publish.

    reply

  • Law Firm Marketing - 6 months, 1 week ago

    this really is great and also meanful. this really is great website. Relating is quite beneficial factor. you might have actually aided a lot of people which check out website and offer these usefull details.

    reply

  • http://www.sytropinbonus.com/ - 6 months, 1 week ago

    Appreciate it intended for giving new messages the matter, When i count on understand far more.

    reply

  • get youtube subscribers - 6 months ago

    This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information...

    reply

  • youtube bots - 6 months ago

    Great Article it its really informative and innovative keep us posted with new updates. its was really valuable. thanks a lot.

    reply

  • this site - 6 months 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.

    this site

    reply

  • hostgator Coupons - 6 months ago

    Great one then we set it to True by default. So I can set your DEBUG value to False on production and then not set anything in your development.

    reply

  • youtube subs - 5 months, 4 weeks ago

    I read your blog frequently and I just thought I’d say keep up the amazing work!

    reply

  • real youtube views - 5 months, 4 weeks ago

    Friend, this web site might be fabolous, i just like it.

    reply

  • Puja Kumari - 5 months, 3 weeks ago

    Thanks for Nice and Informative Post. This article is really contains lot more information about This Topic http://www.top9th.in/packers-and-movers-bangalore/ http://www.top9th.in/packers-and-movers-hyderabad/ http://www.top9th.in/packers-and-movers-mumbai/ http://www.top9th.in/packers-and-movers-delhi/ http://www.top9th.in/packers-and-movers-gurgaon/ http://www.top9th.in/

    reply

  • Puja Kumari - 5 months, 3 weeks ago

    Thanks for post this helpful post - Please visit for More information about: http://www.top9th.in/packers-and-movers-pune/ http://www.top9th.in/packers-and-movers-noida/ http://www.top9th.in/packers-and-movers-chennai/ http://www.top9th.in/packers-and-movers-kolkata/ http://www.top9th.in/packers-and-movers-ahmedabad/ http://www.top9th.in/packers-and-movers-surat/

    reply

  • vikings war of clans hack - 4 months, 1 week ago

    We am seeking to create a few weblogs these days however I possess almost no experience. I wish to find out more to create. Your site appears excellent to me and it good both equally content material and also shape. Your site can help us a lot. However I really hope you are able to assist us straight.

    reply

  • Packers and Movers Noida - 4 months ago

    Thanks for post this helpful post - Please visit for More information about - Packers and Movers in Chennai @ http://www.expert5th.in/packers-and-movers-chennai/ Packers and Movers in Hyderabad @ http://www.expert5th.in/packers-and-movers-hyderabad/ Packers and Movers in Pune @ http://www.expert5th.in/packers-and-movers-pune/ Packers and Movers in Mumbai @ http://www.expert5th.in/packers-and-movers-mumbai/

    reply

  • Packers and Movers Bangalore - 4 months ago

    This article is really fantastic and thanks for sharing the valuable post. Packers and Movers in Gurgaon @ http://www.expert5th.in/packers-and-movers-gurgaon/ Packers and Movers in Delhi @ http://www.expert5th.in/packers-and-movers-delhi/ Packers and Movers in Bangalore @ http://www.expert5th.in/packers-and-movers-bangalore/

    reply

  • Noida - 4 months ago

    Thanks for all your information, Website is very nice and informative content. Packers and Movers in Faridabad @ http://www.expert5th.in/packers-and-movers-faridabad/ Packers and Movers in Noida @ http://www.expert5th.in/packers-and-movers-noida/ Packers and Movers in Thane @ http://www.expert5th.in/packers-and-movers-thane/ Packers and Movers in Navi Mumbai @ http://www.expert5th.in/packers-and-movers-navimumbai/ Packers and Movers in Ghaziabad @ http://www.expert5th.in/packers-and-movers-ghaziabad/

    reply

  • Packers and Movers Pune - 4 months ago

    Thanks for post this helpful post - Please visit for More information about - Packers and movers in Gurgaon @ http://www.dteller.in/packers-and-movers-gurgaon/ Packers and Movers in Bangalore @ http://www.dteller.in/packers-and-movers-bangalore/ Packers and Movers in Hyderabad @ http://www.dteller.in/packers-and-movers-hyderabad/ Packers and Movers in Pune @ http://www.dteller.in/packers-and-movers-pune/

    reply

  • Packers Movers - 4 months ago

    Thanks for post this helpful post - Please visit for More information about:

    http://www.expert9th.in/packers-and-movers-delhi/ http://www.expert9th.in/packers-and-movers-noida/ http://www.expert9th.in/packers-and-movers-gurgaon/ http://www.expert9th.in/packers-and-movers-kolkata/ http://www.expert9th.in/packers-and-movers-ghaziabad/ http://www.expert9th.in/packers-and-movers-amritsar/


    Thanks for post this helpful post - Please visit for More information about:

    http://www.expert9th.in/packers-and-movers-bangalore/ http://www.expert9th.in/packers-and-movers-hyderabad/ http://www.expert9th.in/packers-and-movers-chennai/ http://www.expert9th.in/packers-and-movers-pune/ http://www.expert9th.in/packers-and-movers-faridabad/ http://www.expert9th.in/packers-and-movers-kochi/


    Thanks for post this helpful post - Please visit for More information about:

    http://www.expert9th.in/packers-and-movers-mumbai/ http://www.expert9th.in/packers-and-movers-surat/ http://www.expert9th.in/packers-and-movers-navimumbai/ http://www.expert9th.in/packers-and-movers-ahmedabad/ http://www.expert9th.in/packers-and-movers-chandigarh/ http://www.expert9th.in/packers-and-movers-jamshedpur/

    reply

  • Packers Movers - 4 months ago

    Thanks for Nice and Informative Post. This article is really contains lot more information about This Topic http://www.top9th.in/packers-and-movers-bangalore/ http://www.top9th.in/packers-and-movers-hyderabad/ http://www.top9th.in/packers-and-movers-mumbai/ http://www.top9th.in/packers-and-movers-delhi/ http://www.top9th.in/packers-and-movers-gurgaon/ http://www.top9th.in/


    Thanks for post this helpful post - Please visit for More information about: http://www.top9th.in/packers-and-movers-pune/ http://www.top9th.in/packers-and-movers-noida/ http://www.top9th.in/packers-and-movers-chennai/ http://www.top9th.in/packers-and-movers-kolkata/ http://www.top9th.in/packers-and-movers-ahmedabad/ http://www.top9th.in/packers-and-movers-surat/

    reply

  • Pechhulp - 4 months ago

    Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.

    reply

  •  likesplanet php script - 3 months, 3 weeks 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. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.

    reply

  •  likesplanet php script - 3 months, 3 weeks 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. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.

    reply

  • residential roofing - 3 months, 3 weeks ago

    We will let you to get the free top eleven hack which will help you a lot in enjoying this game very much.

    reply

  • Assignment Help - 1 month, 2 weeks ago

    Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. <a href="https://www.allassignmenthelp.com">Assignment Help</a> | <a href="https://www.allassignmenthelp.com">Assignment Help online</a>

    reply

  • best adjustable beds - 1 month, 2 weeks ago

    I must say, I thought this was a pretty interesting read when it comes to this topic. Liked the material. . .

    reply

  • The Best strategy for binary option trading - 1 month, 2 weeks ago

    blog owners please share. I understand this is off topic nevertheless I simply needed to ask. Thank you!

    reply

  • iPhone cases - 1 month, 1 week ago

    It is delighted to read this post and i get many useful point through this post. Please keep posting such kind of post

    reply

  • Sewa Mobil Surabaya - 1 month, 1 week ago

    HAPPY NEW YEAR 2016 HAPPY NEW YEAR 2016 <br>HAPPY NEW YEAR 2016 HAPPY NEW YEAR 2016

    reply

  • bmw x5 wheels - 1 month ago

    Hey – great blog, just looking around some blogs, seems a really nice platform you are using. I’m currently using WordPress for a few of my blogs but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?

    reply

  • bmw x5 wheels - 1 month ago

    Hey – great blog, just looking around some blogs, seems a really nice platform you are using. I’m currently using WordPress for a few of my blogs but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?

    reply

  • justchill swh - 1 month ago

    Property Management MD MD Property Management Property Management Maryland http://www.renturhome.com

    reply

  • Tischkarten - 2 weeks, 4 days ago

    Very good post... I like it

    reply

  • voyance directe - 2 weeks, 2 days ago

    I was looking at some of your posts on this website and I conceive this web site is really instructive! Keep putting up..

    reply

  • clash of clans hack . pixel gun 3d hack - 2 weeks, 1 day ago

    If you know any others post them in the comments below. Thanks for reading!

    reply

  • happy valentines pictures - 1 week, 2 days ago

    Best valentines day 2016 images, pictures, wallpapers photos, quotes, messages, sms, greetings for whatsapp, facebook sharing

    reply

  • valentines day text messages for her - 1 week, 2 days ago

    Best valentines day 2016 images, pictures, wallpapers photos, quotes, messages, sms, greetings for whatsapp, facebook sharing

    reply