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

enter image description here

        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
            )
Asked By: lion-react

||

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,
)
Answered By: AKX
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.