Python Road 70-django Signal

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.