Directory
First, Introduction
Second, built-in signal
Iii.. Custom Signals
First, Introduction
Django provides a "signal dispatch" for decoupling when the framework performs operations. In layman's terms, when something happens, the signal allows a specific sender to remind some recipients
Second, built-in signal
Django built-in signal
model signals pre_init # django modal automatic trigger before executing its construction method post_init # django modal automatically triggers after its construction method is executed pre_save # The Django Modal object is automatically triggered before it is saved post_save # django Modal object is saved, auto trigger pre_delete Auto Trigger post_delete before # django modal object is deleted &nBsp; # django modal object is deleted and automatically triggered m2m_changed # Automatically trigger class_prepared before and after the third table (Add,remove,clear) in Django modal using the Auto-Work field # when the program starts, detects the modal class in the registered app, for each class, Auto Trigger management signals pre_migrate # automatically triggers post_ before executing the migrate command migrate # Automatically triggers request/response signals request_started after executing the migrate command Automatic triggering of request_ before # request arrives finished # automatically triggers got_request_exception after the request is completed # automatically triggers test signals setting_ after an exception is requested changed # When you modify a configuration file using test testing, Auto Trigger template_rendered # automatically triggers database wrappers connection_created when a render template is tested with test # automatically triggers when a database connection is created
For Django built-in signals, only the specified signal needs to be registered, and the registration function is triggered automatically when the program executes the response operation.
Create a file named sg.py below the project and import it in the project __init__.py
Import SG
This will automatically load
from django.core.signals import request_finished from Django.core.signals import request_started from django.core.signals import got_request_exception from django.db.models.signals import class_prepared from django.db.models.signals import pre_init, post_init from django.db.models.signals import pre_save, post _save from django.db.models.signals import pre_delete, post_delete from django.db.models.signals import m2m_changed from django.db.models.signals import pre_migrate, post_migrate from django.test.signals import setting_changed from Django.test.signals import template_rendered from django.db.backends.signals import connection_created def callback (Sender, **kwargs): print (" Xxoo_callback ") print (Sender,kwargs) Xxoo.connect (callback) # xxoo refers to the contents of the above import
views.py
From django.core.signals import request_finishedfrom django.dispatch import Receiver@receiver (request_finished) def my _callback (sender, **kwargs): Print ("Request finished!")
Iii.. Custom Signals
1. Defining the Signal
Import Django.dispatchpizza_done = django.dispatch.Signal (providing_args=["toppings", "size"])
2. Registration signal
DEF callback (sender, **kwargs): Print ("callback") print (Sender,kwargs) Pizza_done.connect (callback)
3. Trigger Signal
From path import pizza_done pizza_done.send (sender= ' Seven ', toppings=123, size=456)
Since the trigger for the built-in signal is already integrated into Django, it is automatically invoked, and for custom signals the learner developer triggers the
This article is from the "Eight Miles" blog, so be sure to keep this source http://5921271.blog.51cto.com/5911271/1930264
Python Road 70-django Signal