我想在Django的Send_mail函数中使用“if和elif”

iih3973s  于 2023-03-24  发布在  Go
关注(0)|答案(1)|浏览(86)

我想实现的是我想在Django的Send_mail函数中使用“if和elif”,但是发生了一个错误,我不知道如何修复它。
错误信息

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
            )
6l7fqoea

6l7fqoea1#

你不能在调用表达式中放入if套件。你可以使用条件运算符(x if y else z),但只使用常规的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,
)

相关问题