1.目录结构
2.在Django/singals.py目录下编写?
# 系统自带的信号
from django.core.signals import request_finished
from django.db.models.signals import pre_save,post_save
from django.dispatch import receiver
@receiver(pre_save)
def pre_save_func(sender,**kwargs):
print("pre_save_func")
print("pre_save_msg:",sender,kwargs)
@receiver(post_save)
def post_save_func(sender,**kwargs):
print("post_save_func")
print("post_save_msg:",sender,kwargs)
@receiver(request_finished)
def callback(sender, **kwargs):
print("Request finished!")
# 自定义信号
import django.dispatch
# providing_args是发送信号时传递给的参数,最后都会被kwargs接收
custom_signal=django.dispatch.Signal(providing_args=["aaaa","bbbb"])
def pre_save_func2(sender,**kwargs):
print("pre_save_func")
print("pre_save_msg:",sender,kwargs)
custom_signal.connect(pre_save_func2)
3.将custom_signal自定义信号对象加入到Django/__init__.py中
4.自定义的需要手动触发信号,系统自带的,会自动触发
响应结果结果:
?
?
|