1.目录结构
2.在Django/singals.py目录下编写
# 系统自带的信号from django.core.signals import request_finishedfrom django.db.models.signals import pre_save,post_savefrom 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.自定义的需要手动触发信号,系统自带的,会自动触发
响应结果结果: