I\'m writing a Django Middleware class that I want to execute only once at startup, to initialise some other arbritary code. I\'ve followed the very nice solution posted by
This question is well-answered in the blog post Entry point hook for Django projects, which will work for Django >= 1.4.
Basically, you can use to do that, and it will be run only once, when the server starts, but not when you run commands or import a particular module.
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
# Run startup code!
....
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()