Reputation: 34934
It seems Django allows to execute the code on startup - when the app starts, however, it's not clear and where I should put the code. So how can I execute the code on startup in Django 1.7?
Upvotes: 7
Views: 6631
Reputation: 59435
For Django>=1.7 you can use the AppConfig.ready()
callback:
https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.ready
For previous versions, see this answer.
If you are using the AppConfig.ready()
method:
1) Create a myapp/apps.py
module and subclass the AppConfig. For example:
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'myapp'
def ready(self):
...
2) Edit myapp/__init__.py
and register your app config:
default_app_config = 'myapp.apps.MyAppConfig'
See https://docs.djangoproject.com/en/1.7/ref/applications/#configuring-applications for details.
Upvotes: 12