我正在編寫一個信號函數(shù),該函數(shù)將在用戶注冊時向用戶發(fā)送注冊通知電子郵件,并在她/他更新用戶個人資料時發(fā)送更新通知電子郵件。為此,我使用了 if-else 塊來檢查created參數(shù)。如果為真,用戶將收到注冊通知。否則,用戶將收到配置文件更新通知。更新用戶對象時它工作正常。但是當(dāng)新用戶注冊時,if 和 else 塊都在執(zhí)行,并且新注冊的用戶會收到兩封電子郵件通知:一封注冊通知和一封個人資料更新通知。完整的信號代碼如下:#myapp/signals.pyfrom django.core.mail import send_mailfrom django.db.models.signals import post_savefrom django.dispatch import receiverfrom django.template.loader import render_to_stringfrom django.utils.html import strip_tags#getting custom user modelUser = get_user_model()@receiver(post_save, sender=User)def send_user_notification(sender, instance, created, **kwargs): if created: subject = 'Signup Notification' html_message = render_to_string( 'email/signup_notification.html', { 'username': instance.username, 'email': instance.email, } ) message = strip_tags(html_message) send_mail(subject, message, from_email='info@silatechnologies.com', recipient_list=[instance.email,], html_message=html_message) else: send_mail(subject='Your profile on Sila Crowdfunding updated!', message='Dear ' + instance.username + ', your profile information is updated successfully.', from_email='info@silatechnologies.com', recipient_list=[instance.email, ] )我也用過if not created代替else。但我得到了同樣的結(jié)果。任何人都可以提供任何解決方案嗎?提前致謝。
為什么在 Django 信號中如果塊為真而執(zhí)行其他塊?
ibeautiful
2022-01-18 16:02:52