Django views.py invalid syntax

二次信任 提交于 2019-12-25 08:57:58

问题


I want to send email to multiple recipients in Django, using SendGrid.

In my views.py file I have this and it works:

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

But I want to add addresses from loop. So, I use:

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

and I get the following error:

    for address in addresses:
      ^
SyntaxError: invalid syntax

What is the correct syntax?


回答1:


You can try list comprehension

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


来源:https://stackoverflow.com/questions/44260033/django-views-py-invalid-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!