I am attempting to iterate through a map containing arrays to an HTML email template. I can print out the data in the array but, the data does not show up in the array after finishing the loop.
html_data = """
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<br>
<br>
Date: {{date}}
<br>
<br>
Hi,
<br>
<br>
Today's Picks Are in:
<br>
<br>
<div >
<table >
<thead>
<tr>
<th align='left'>Stock</th>
<th>Pick</th>
<th>Detail</th>
<th>Change</th>
</tr>
</thead>
<tbody>
<td align='left'>{{ picks }}</td>
{% for pick in picks %}
<tr>
<td align='center'> {{pick.stock}} </td>
# <td align='center'> {{pick.prices}} </td>
# <td align='center'>{{pick.option_details}}</td>
# <td align='center'>{{pick.change}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<br>
<br>
<p>If you would like to be removed from this list, please contact customer service: <mail_to="devs@hscottindustriescom">HScottIndustries</a> </p>
"""
Picks is the mapped array
picks = { 'stocks': stocks, 'prices': stock_price, 'option_details': options_details, 'change': change }
The data from picks is added here.
msg = MIMEText(Environment().from_string(html_data).render(picks=picks, date=date_original), "html")
The output is this:
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<br>
<br>
Date: 07-05-2022
<br>
<br>
Hi,
<br>
<br>
Today's Picks Are in:
<br>
<br>
<div >
<table >
<thead>
<tr>
<th align='left'>Stock</th>
<th>Pick</th>
<th>Detail</th>
<th>Change</th>
</tr>
</thead>
<tbody>
<td align='left'>{'stocks': ['WPP', 'NEX', 'PTEN'], 'prices': ['45.21', '8.37', '13.39'], 'option_details': ['na', 'NEX Jul 15 2022 $7.50 Put', 'PTEN Jul 15 2022 $13.00 Put'], 'change': ['-10.19%', '-11.52%', '-11.53%']}</td>
<tr>
<td align='center'> </td>
# <td align='center'> </td>
# <td align='center'></td>
# <td align='center'></td>
</tr>
<tr>
<td align='center'> </td>
# <td align='center'> </td>
# <td align='center'></td>
# <td align='center'></td>
</tr>
<tr>
<td align='center'> </td>
# <td align='center'> </td>
# <td align='center'></td>
# <td align='center'></td>
</tr>
<tr>
<td align='center'> </td>
# <td align='center'> </td>
# <td align='center'></td>
# <td align='center'></td>
</tr>
</tbody>
</table>
</div>
<br>
<br>
<p>If you would like to be removed from this list, please contact customer service: <a href="https://hscottindustriescom">HScottIndustries</a> </p>
<footer >
<strong>Copyright © 2022 <mail_to="[email protected]">HScottIndustries LLC</a></strong>
How do I add the get the data to iterate correctly to the corresponding fields?
CodePudding user response:
You are iterating over picks, which right now is a dictionary, meaning you will get dictionary keys in return. You can either format picks in a different way, or use an index. This format should work:
picks = [
{
'stocks': 'WPP',
'prices': '45.21',
'option_details': 'na',
'change': '-10.19%'
},
{
'stocks': 'NEX',
'prices': ...
},
...
]
Alternatively you can pass picks as zipped arrays in your view:
zipped_picks = zip(*picks.values())
Then access them with:
{% for pick in zipped_picks %}
<div> {{pick.0}} <div>
<div> {{pick.1}} <div>
<div> {{pick.2}} <div>
<div> {{pick.3}} <div>
{% endfor %}