问题
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