tags:

views:

217

answers:

7

I would like to run some environment checks when my django process starts and die noisily in the case of an error. I'm thinking things like the database has an incorrect encoding or the machine has a python version we don't support.

I'd rather our team be faced with a fatal error that they have to fix, rather than be able to ignore it.

I'm Ok with writing these checks but I'm curious about where the best place to put them is. How do I get them to execute as part of django's startup process? I thought there might be a signal I could listen too, but I can't find a relevant one in the docs.

+3  A: 

I would put them in settings.py. In the past, I have put system checks like this:

try:
    from local_settings import *
except ImportError:
    print "Missing %s" % os.path.join(PROJECT_ROOT, "local_settings.py")

if DEBUG:
    for p in [PROJECT_ROOT, MEDIA_ROOT, THEME_DIR, ADMIN_MEDIA_ROOT] + list(TEMPLATE_DIRS):
        p = os.path.normpath(p)
        if not os.path.exists(p):
            print "Missing path: %s" % p
hughdbrown
I can see that it'd work, however mixing executable code and configuration feels "wrong".
James Healy
SUre, I'll buy that. PiotrLegnica's suggestion of \_\_init\_\_.py sounds good, although I doubt you'll get the initialization done in settings.py done at the time it is loaded.
hughdbrown
`settings.py` would be my choice, too, for this safety check. i thought there was a post-model-validation signal (the kind of model validation optionally done by a management command), but i couldn't find one in the source.
Carson
+1  A: 

If you don't want to use settings module, then try project's __init__.py.

PiotrLegnica
+3  A: 

If you want to check that the system is correctly installed, I think that you should write your own admin command and run it as post-installation check.

I think that it doesn't worth to check if the python version is correctly installed too often especially if you are installing the django app on shared-host. My app is hosted at alwaysdata and they restart the FastCgi process every hour. These checks can have an impact on the application response time.

luc
+1  A: 

We use the top-level urls.py for this.

S.Lott
A: 

The best place to put this is into a Middleware class. Just use the Middleware to check things out before your page loads and raise an error if the check fails.

orokusaki
-1 This runs checks on every single request, which is really serious overkill (and performance hit) for system configuration checks.
Carl Meyer
See http://stackoverflow.com/questions/2781383/where-to-put-django-startup-code for a trick that gets round that problem
andybak
A: 

See http://stackoverflow.com/questions/2781383/where-to-put-django-startup-code

edw
in other words, orokusaki answer is quite good, imho
edw
A: 

You can put it in settings.py as mentioned by others, but having code in the settings is not ideal. There is also the option of adding a handler for django.db.models.signals.class_prepared that does the desired start up checks after a specific model class is prepared.