欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

django之信号

时间:2023-06-01

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.自定义的需要手动触发信号,系统自带的,会自动触发

响应结果结果:

 

 

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。