I want to use "if and elif" Inside the Send_mail function in Django
Question:
What I want to achieve is
I want to use "if and elif" Inside the Send_mail function in Django
but an error occurs and I don’t know how to fix it…
Error message
potential arugment cannot appear after keyword aurgument
send_mail(
subject='New message',
message='Access to http://localhost:3000/messagerooms',
from_email=settings.EMAIL_HOST_USER,
if inquiry_user == self.request.user:
recipient_list = post.user.email
elif post.user == self.request.user:
recipient_list = inquiry_user.email
)
Answers:
You can’t put an if
suite inside a call expression. You could use a conditional operator (x if y else z
), but it’s easier to use just a regular if
:
if inquiry_user == self.request.user:
recipient_list = [post.user.email]
elif post.user == self.request.user:
recipient_list = [inquiry_user.email]
else:
raise ValueError("Can't determine recipient")
send_mail(
subject="New message",
message="Access to http://localhost:3000/messagerooms",
from_email=settings.EMAIL_HOST_USER,
recipient_list=recipient_list,
)
What I want to achieve is
I want to use "if and elif" Inside the Send_mail function in Django
but an error occurs and I don’t know how to fix it…
Error message
potential arugment cannot appear after keyword aurgument
send_mail(
subject='New message',
message='Access to http://localhost:3000/messagerooms',
from_email=settings.EMAIL_HOST_USER,
if inquiry_user == self.request.user:
recipient_list = post.user.email
elif post.user == self.request.user:
recipient_list = inquiry_user.email
)
You can’t put an if
suite inside a call expression. You could use a conditional operator (x if y else z
), but it’s easier to use just a regular if
:
if inquiry_user == self.request.user:
recipient_list = [post.user.email]
elif post.user == self.request.user:
recipient_list = [inquiry_user.email]
else:
raise ValueError("Can't determine recipient")
send_mail(
subject="New message",
message="Access to http://localhost:3000/messagerooms",
from_email=settings.EMAIL_HOST_USER,
recipient_list=recipient_list,
)