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, 5 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, 5 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, 5 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, 5 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 - 4 months, 2 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 - 4 months, 1 week ago

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

    reply

  • hill climb racing cheat - 4 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.

    reply

  • spinner - 3 months, 3 weeks ago

    spinner

    reply

  • essay service reviews - 3 months, 3 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.

    reply

  • Telestep ladders - 3 months, 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.

    reply

  • visitwebsite - 2 months 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 - 2 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.

    reply

  • 100 comments - 2 months 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 - 2 months 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 - 2 months 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 - 2 months 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 - 2 months ago

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

    reply

  • Credit Counseling - 1 month, 3 weeks 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 - 1 month, 3 weeks 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 - 1 month, 3 weeks 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 - 1 month, 3 weeks 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 - 1 month, 3 weeks 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 - 1 month, 2 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 - 1 month, 2 weeks ago

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

    reply

  • get free instagram likes - 1 month, 2 weeks ago

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

    reply

  • buy facebook likes - 1 month, 1 week 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 - 1 month, 1 week 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 - 4 weeks, 1 day 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 - 4 weeks, 1 day 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 - 1 month, 1 week 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 - 1 month, 1 week 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 - 1 month, 1 week 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 - 1 month, 1 week 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 - 1 month, 1 week 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 - 1 month 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 - 1 month ago

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

    reply

  • catering - 1 month 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 - 1 month 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 - 1 month 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/ - 1 month ago

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

    reply

  • get youtube subscribers - 3 weeks, 5 days 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 - 3 weeks, 5 days 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 - 3 weeks, 5 days 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 - 3 weeks, 3 days 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 - 2 weeks, 6 days ago

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

    reply

  • real youtube views - 2 weeks, 6 days ago

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

    reply

  • Puja Kumari - 1 week, 5 days 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 - 1 week, 5 days 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