为什么 else 块在 Django 信号中执行而 if 块为真?
Why else block is executing while if block is true in Django signals?
我正在编写一个信号函数,它将在 s/he 注册时向用户发送注册通知电子邮件,并在 s/he 更新用户个人资料时向用户发送更新通知电子邮件。为此,我使用了 if-else 块来检查 created
参数。如果为真,用户将收到注册通知。否则,用户将收到配置文件更新通知。更新用户对象时它工作正常。但是当新用户注册时,if 和 else 块都在执行,新注册用户会收到两封电子邮件通知:一封注册通知和一封个人资料更新通知。完整的信号代码如下:
#myapp/signals.py
from django.core.mail import send_mail
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.utils.html import strip_tags
#getting custom user model
User = 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
。但我得到了相同的结果。任何人都可以提供任何解决方案吗?提前致谢。
更新:
我的注册用户查看功能可能对解决问题有帮助。这是视图函数:
def signup(request):
"""
Render signup form if request is GET else create user
"""
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('home')
else:
form = SignUpForm()
return render(request, 'registration/signup.html', {'form': form})
假设您正在使用 Django 的内置注册流程,这将意味着在 User
模型上执行 2 个操作:
- 它将创建
User
对象,从而用 created=True
触发 post_save
信号,您将发送第一封电子邮件。
- 注册后,Django 登录用户和 updates it's
last_login
字段,从而用 created=False
触发另一个 post_save
信号
我正在编写一个信号函数,它将在 s/he 注册时向用户发送注册通知电子邮件,并在 s/he 更新用户个人资料时向用户发送更新通知电子邮件。为此,我使用了 if-else 块来检查 created
参数。如果为真,用户将收到注册通知。否则,用户将收到配置文件更新通知。更新用户对象时它工作正常。但是当新用户注册时,if 和 else 块都在执行,新注册用户会收到两封电子邮件通知:一封注册通知和一封个人资料更新通知。完整的信号代码如下:
#myapp/signals.py
from django.core.mail import send_mail
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.utils.html import strip_tags
#getting custom user model
User = 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
。但我得到了相同的结果。任何人都可以提供任何解决方案吗?提前致谢。
更新:
我的注册用户查看功能可能对解决问题有帮助。这是视图函数:
def signup(request):
"""
Render signup form if request is GET else create user
"""
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('home')
else:
form = SignUpForm()
return render(request, 'registration/signup.html', {'form': form})
假设您正在使用 Django 的内置注册流程,这将意味着在 User
模型上执行 2 个操作:
- 它将创建
User
对象,从而用created=True
触发post_save
信号,您将发送第一封电子邮件。 - 注册后,Django 登录用户和 updates it's
last_login
字段,从而用created=False
触发另一个
post_save
信号