You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django邮件模板无法渲染全部订单商品的问题及解决

问题:邮件模板仅显示单个订单商品,无法渲染全部商品信息

我尝试将与list_ordered_items关联的所有商品名称、价格和数量渲染到邮件模板中,但邮件仅显示单个商品详情,而非多个商品信息——我已在视图中进行循环!请问问题原因是什么?该如何修复?

相关代码

Models

class Product(models.Model):
    name = models.CharField()
    final_price =  models.DecimalField()

class OrderItem(models.Model):
    product = models.ForeignKey(Product)
    quantity = models.IntegerField(default=0)

Views

list_ordered_items = OrderItem.objects.filter(order__customer__user=request.user).select_related('product')

for loi in list_ordered_items:
    product_name = loi.product.name
    product_price = loi.product.final_price
    product_qty = loi.quantity
    context = {'product_name': product_name, 'product_qty': product_qty, 'product_price': product_price}

邮件模板

<tr>
    <td align="left" style="padding:0;Margin:0"><h4 style="Margin:0;line-height:17px;mso-line-height-rule:exactly;font-family:arial, 'helvetica neue', helvetica, sans-serif;font-size:14px"><strong></strong>{{product_name}}<strong></strong></h4></td>
</tr>
<tr>
    <td style="padding:0;Margin:0;text-align:center;font-size:13px;line-height:13px" width="15%" align="center"><p style="Margin:0;-webkit-text-size-adjust:none;-ms-text-size-adjust:none;mso-line-height-rule:exactly;font-family:arial, 'helvetica neue', helvetica, sans-serif;line-height:21px;color:#333333;font-size:14px"><strong></strong>{{product_qty}}<strong></strong></p></td>
    <td style="padding:0;Margin:0;text-align:center;font-size:13px;line-height:13px" width="30%"><p style="Margin:0;-webkit-text-size-adjust:none;-ms-text-size-adjust:none;mso-line-height-rule:exactly;font-family:arial, 'helvetica neue', helvetica, sans-serif;line-height:21px;color:#333333;font-size:14px"><strong></strong>${{product_price | floatformat:2}}<strong></strong></p></td>
</tr>

当前显示效果

当前仅显示单个商品

预期显示效果

显示全部订单商品


原因分析

视图中的循环逻辑错误:每次遍历订单项时,都会重新创建context字典并覆盖之前的数据,最终context里只保留了最后一个订单商品的信息。同时模板中没有遍历商品列表的逻辑,自然只会显示单个商品。

修复方案

1. 修改视图代码

不需要手动遍历订单项,直接将查询集传入context即可:

list_ordered_items = OrderItem.objects.filter(order__customer__user=request.user).select_related('product')
context = {'ordered_items': list_ordered_items}

2. 修改邮件模板

在模板中使用Django模板语法的for循环,遍历传入的订单项列表,渲染每一个商品的信息:

{% for item in ordered_items %}
<tr>
    <td align="left" style="padding:0;Margin:0"><h4 style="Margin:0;line-height:17px;mso-line-height-rule:exactly;font-family:arial, 'helvetica neue', helvetica, sans-serif;font-size:14px"><strong></strong>{{item.product.name}}<strong></strong></h4></td>
</tr>
<tr>
    <td style="padding:0;Margin:0;text-align:center;font-size:13px;line-height:13px" width="15%" align="center"><p style="Margin:0;-webkit-text-size-adjust:none;-ms-text-size-adjust:none;mso-line-height-rule:exactly;font-family:arial, 'helvetica neue', helvetica, sans-serif;line-height:21px;color:#333333;font-size:14px"><strong></strong>{{item.quantity}}<strong></strong></p></td>
    <td style="padding:0;Margin:0;text-align:center;font-size:13px;line-height:13px" width="30%"><p style="Margin:0;-webkit-text-size-adjust:none;-ms-text-size-adjust:none;mso-line-height-rule:exactly;font-family:arial, 'helvetica neue', helvetica, sans-serif;line-height:21px;color:#333333;font-size:14px"><strong></strong>${{item.product.final_price | floatformat:2}}<strong></strong></p></td>
</tr>
{% endfor %}

内容的提问来源于stack exchange,提问作者betty_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 13:05:17