Django views.py语法无效

时间:2017-05-30 10:44:03

标签: python django sendgrid

我想使用SendGrid向Django中的多个收件人发送电子邮件。

在我的 views.py 文件中,我有这个并且有效:

data = {
    "personalizations": [
        {
            "to": [
                {"email": "address@example.com"},
                {"email": "address2@example.com"},
            ],
            "subject": "New message
        }
    ],
    "from": {
        "email": email
    },
    "content": [
        {
            "type": "text/plain",
            "value": message
        }
    ]
}

但我想从循环中添加地址。所以,我用:

"to": [
    for address in addresses:
        {"email": address},
],

我收到以下错误:

    for address in addresses:
      ^
SyntaxError: invalid syntax

正确的语法是什么?

1 个答案:

答案 0 :(得分:4)

您可以尝试list comprehension

"to": [{"email": address} for address in addresses]