是否可以在Sendgrid模板中包含jinja2模板?
我正在尝试在Sendgrid模板中使用{% for %}
和{% endfor %}
,但似乎不起作用。虽然电子邮件发送成功,但是在我打开电子邮件时为空。
HTML代码(placeorder.html):
<body>
<h3>From: {{name}}</h3>
<h3>Email: {{emailFrom}}</h3>
<table id="customers">
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Sub Total</th>
</tr>
{% for order_item in order.items.all %}
<tr>
<td>{{ order_item.item.title}}</td>
<td>{{ order_item.quantity }}</td>
<td>₦{{ order_item.get_final_price }}</td>
</tr>
{% endfor %}
<p>
<span>Total ₦</span>
<strong>{{ order.get_total }}</strong>
</p>
</table>
</body>
Views.py(Django)
def placeorder(request):
form = orderform(request.POST)
try:
order = Order.objects.get(user=request.user, ordered=False)
if form.is_valid():
order = Order.objects.get(user=request.user, ordered=False)
name = form.cleaned_data['name']
emailFrom = form.cleaned_data['email']
emailTo = settings.DEFAULT_FROM_EMAIL
message = Mail(
from_email=emailFrom,
to_emails= emailTo ,
subject='subject',
html_content= settings.BASE_DIR + "/templates/placeorder.html")
message.dynamic_template_data = {
'name': name,
'email': emailFrom ,
}
message.template_id = '********************************'
try:
sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
response = sg.send(message)
messages.success(request, "Thanks, We received your Order message. We will get back to you!.")
OrderItem.delete(order)
except Exception as e:
messages.warning(request, "Sorry, Order message not sent.Please Try Again!.")
return render(request, "checkout.html", context)