Automatically setting 'debug' in your Django app based on server hostname.

Posted 8 months ago in Django

I've done a couple of Django apps so far, and one thing that always annoys me is having to manually set DEBUG in my settings.py when moving from development to production, or vice versa.

Since I'm always developing on my localhost, I know that I always want DEBUG to be True when running on localhost. I also know that DEBUG should always be False when running on my production server. Fortunately, Python makes quick work of this. In your settings.py file:

That's pretty much it. Saves me a ton of time.

Yes, I'm that lazy.

6 Comments

8 months ago

DEBUG = socket.gethostname() != 'productionserver.com'

Since these could be more than one machine, I add this in my settings.py:

import platform

DEPLOYMENT_SERVERS = ('server1.com', 'bla.blub.com',)
DEBUG = platform.node() not in DEPLOYMENT_SERVERS

You see, I'm lazy, too ;-)

8 months ago

Ah, interesting! Very cool :)

Thanks!

atli
8 months ago

We have a lot of settings that are different in deployment and development so we created a file called localsettings.py, set DEBUG=True and whatever else you need, make sure it's ignored by your version control system.
Add this to the end of your settings.py file

try:
import localsettings
except:
pass

8 months ago

Eric wrote a great blog post on extending debug functionality to production servers based on admin status or IP. Check it out!

Nick

8 months ago

yeah, I'm just using a separate local_settings.py, but this is a cool idea (you're not the only one who's that lazy, trust me)

5 months ago

I'll try this code in my project

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options