views:

316

answers:

5

How can I be certain that my application is running on development server or not? I suppose I could check value of settings.DEBUG and assume if DEBUG is True then it's running on development server, but I'd prefer to know for sure than relying on convention.

+1  A: 

settings.DEBUG could be True and running under Apache or some other non-development server. It will still run. As far as I can tell, there is nothing in the run-time environment short of examining the pid and comparing to pids in the OS that will give you this information.

hughdbrown
A: 

One difference between the development and deployment environment is going to be the server that it’s running on. What exactly is different will depend on your dev and deployment environments.

Knowing your own dev and deploy environments, the HTTP request variables could be used to distinguish between the two. Look at request variables like request.META.HTTP_HOST, request.META.SERVER_NAME and request.META.SERVER_PORT and compare them in the two environments.

I bet you’ll find something quite obvious that’s different and can be used to detect your development environment. Do the test in settings.py and set a variable that you can use elsewhere.

Nate
+5  A: 
server = request.META.get('wsgi.file_wrapper', None)
if server is not None and server.__module__ == 'django.core.servers.basehttp':
    print 'inside dev'

Of course, wsgi.file_wrapper might be set on META, and have a class from a module named django.core.servers.basehttp by extreme coincidence on another server environment, but I hope this will have you covered.

By the way, I discovered this by making a syntatically invalid template while running on the development server, and searched for interesting stuff on the Traceback and the Request information sections, so I'm just editing my answer to corroborate with Nate's ideas.

inerte
+1 for actually trying to address the question i.e. detecting what server is serving up the Django app, rather than relying on settings. For example, there is nothing stopping someone from running in DEBUG mode behind something other than the development web server.
Ryan Duffield
Another META field that is specific for dev server: SERVER_SOFTWARE has strings `WSGIServer` and `Python` on dev and anything you configure in your HTTP server on "deployed".
zgoda
I would add a `getattr(server, '__module__', None)` in the second line instead of going directly to the dot notation. You never know...
Tal Weiss
Very cool, but I would like to do this once upon startup, and not upon every request. Is that possible?
Tal Weiss
+4  A: 

Typically I set an variable called environment and set it to "DEVELOPMENT", "STAGING" or "PRODUCTION". Within the settings file I can then add basic logic to change which settings are being used, based on environment.

EDIT: Additionally, you can simply use this logic to include different settings.py files that override the base settings. For example:

if environment == "DEBUG":
    from debugsettings import *
Soviut
A: 

Relying on settings.DEBUG is the most elegant way AFAICS as it is also used in Django code base on occasion.

I suppose what you really want is a way to set that flag automatically without needing you update it manually everytime you upload the project to production servers.

For that I check the path of settings.py (in settings.py) to determine what server the project is running on:

if __file__ == "path to settings.py in my development machine":
    DEBUG = True
elif __file__ in [paths of production servers]:
    DEBUG = False
else:
    raise WhereTheHellIsThisServedException()

Mind you, you might also prefer doing this check with environment variables as @Soviut suggests. But as someone developing on Windows and serving on Linux checking the file paths was plain easier than going with environment variables.

utku_karatas