How can I tell whether my Django application is ru

2019-01-17 02:11发布

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.

12条回答
The star\"
2楼-- · 2019-01-17 02:52

If you want to switch your settings files automatically dependent on the runtime environment you could just use something that differs in environ, e.g.

from os import environ
if environ.get('_', ''): 
    print "This is dev - not Apache mod_wsgi"         
查看更多
手持菜刀,她持情操
3楼-- · 2019-01-17 02:53

I came across this problem just now, and ended up writing a solution similar to Aryeh Leib Taurog's. My main difference is that I want to differentiate between a production and dev environments when running the server, but also when running some one-off scripts for my app (which I run like DJANGO_SETTINGS_MODULE=settings python [the script] ). In this case, simply looking at whether argv[1] == runserver isn't enough. So what I came up with is to pass an extra command-line argument when I run the devserver, and also when I run my scripts, and just look for that argument in settings.py. So the code looks like this:

if '--in-development' in sys.argv:
    ## YES! we're in dev
    pass
else:
    ## Nope, this is prod
    pass

then, running the django server becomes

python manage.py runserver [whatever options you want] --in-development

and running my scripts is as easy as

DJANGO_SETTINGS_MODULE=settings python [myscript] --in-development

Just make sure the extra argument you pass along doens't conflict with anything django (in reality I use my app's name as part of the argument). I think this is pretty decent, as it lets me control exactly when my server and scripts will behave as prod or dev, and I'm not relying on anyone else's conventions, other than my own.

EDIT: manage.py complains if you pass unrecognized options, so you need to change the code in settings.py to be something like

if sys.argv[0] == 'manage.py' or '--in-development' in sys.argv:
    # ...
    pass

Although this works, I recognize it's not the most elegant of solutions...

查看更多
smile是对你的礼貌
4楼-- · 2019-01-17 02:55

Typically I set a 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 *
查看更多
我命由我不由天
5楼-- · 2019-01-17 02:58

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.

查看更多
不美不萌又怎样
6楼-- · 2019-01-17 02:58

I use:

DEV_SERVERS = [
    'mymachine.local',
]

DEVELOPMENT = platform.node() in DEV_SERVERS

which requires paying attention to what is returned by .node() on your machines. It's important that the default be non-development so that you don't accidentally expose sensitive development information.

You could also look into more complicated ways of uniquely identifying computers.

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-01-17 03:01

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.

查看更多
登录 后发表回答