Skip to content Skip to sidebar Skip to footer

Correct Place To Put Extra Startup Code In Django?

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 o

Solution 1:

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

Solution 2:

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.

Solution 3:

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

Solution 4:

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)
        ifnotos.path.exists(p):
            print"Missing path: %s" % p

Solution 5:

I have tested all three __init__.py Settings.py and urls.py methods and this is what I have found.

When code is run from either __init__.py or Settings.py, the start up functions are run twice upon web server start up; when the start up functions are run from urls.py the code is run once, however it is run only upon the first request made to the we server leading to a potentially long wait for the first user to visit your site.

It is standard practise to call a 'warming' page when bringing large web applications back online so I don't see that calling a start up function from a CLEARLY identified location in the urls.py should be a problem.

Post a Comment for "Correct Place To Put Extra Startup Code In Django?"