The alpha of Django 1.7 was released recently and I’ve started to experiment with the new applications feature.
Applications include some combination of models, views, templates, template tags, static files, URLs, middleware, etc. They’re generally wired into projects with the INSTALLED_APPS setting and optionally with other mechanisms such as URLconfs, the MIDDLEWARE_CLASSES setting, or template inheritance.
The power of this feature is that it allows you to create an application configuration class which has a ready method, allowing you to perform initialization tasks such as registering signals when the application first loads.
To get started with applications, set the default_app_config variable in your application’s __init__.py:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# myapp/__init__.py | |
default_app_config = 'myapp.apps.MyAppConfig' |
Now create a new file called apps.py in the root of your application and create a class that inherits from AppConfig:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# myapp/apps.py | |
from django.apps import AppConfig | |
class MyAppConfig(AppConfig): | |
name = 'myapp' | |
verbose_name = 'My App' | |
def ready(self): | |
# import signal handlers | |
import myapp.signals.handlers |
Within this class you can define a variety of things including the application’s name, verbose name and the ready method. In the example above, I’m importing the signals submodule that contains the signal receivers (in Django 1.6 and below, signal registration usually happened in the models module).
Finally, here’s an example signal and handler from my signals submodule:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# myapp/signals/signals.py | |
from django.dispatch import Signal | |
example_signal = Signal(providing_args=['arg1', 'arg2']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# myapp/signals/handlers.py | |
from django.dispatch import receiver | |
from .signals import example_signal | |
@receiver(example_signal) | |
def example_signal_handler(sender, **kwargs): | |
print kwargs['arg1'] | |
print kwargs['arg2'] |
Hi Chris,
Very useful but how do I know when is the signal (example_signal) sent/emitted?
Reading your article and docs https://docs.djangoproject.com/en/1.9/ref/applications/#django.apps.AppConfig
I tried to add myAppConfig but I get this error
django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet.
Do you know something about that??
Most likely this is due to a 3rd party app that you have in your INSTALLED_APPS that isn’t compatible with Django 1.9.
You should mention that default_app_config is not for new apps, new apps should use a dotted path to AppConfig subclass in INSTALLED_APPS instead:
https://docs.djangoproject.com/en/1.9/ref/applications/#configuring-applications
Very useful post Thanks. It is actually worth to locate signals in a specific file.