- Django 2 Web Development Cookbook
- Jake Kronika Aidas Bendoraitis
- 120字
- 2021-06-10 19:31:30
How to do it...
Follow these steps to create and use the app configuration:
- Create the apps.py file and put the following content in it, as follows:
# magazine/apps.py
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _
class MagazineAppConfig(AppConfig):
name = "magazine"
verbose_name = _("Magazine")
def ready(self):
from . import signals
- Edit the __init__.py file in the magazine module to contain the following content:
# magazine/__init__.py
default_app_config = "magazine.apps.MagazineAppConfig"
- Let's create a signals.py file and add some signal handlers there:
# magazine/signals.py
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.conf import settings
from .models import NewsArticle
@receiver(post_save, sender=NewsArticle)
def news_save_handler(sender, **kwargs):
if settings.DEBUG:
print(f"{kwargs['instance']} saved.")
@receiver(post_delete, sender=NewsArticle)
def news_delete_handler(sender, **kwargs):
if settings.DEBUG:
print(f"{kwargs['instance']} deleted.")