You need to enable JavaScript to run this app. Python+Django电商项目checkout页面无法渲染cart页可正常显示的数据怎么办
优惠活动
大模型
产品
解决方案
定价
更多

Python+Django电商项目checkout页面无法渲染cart页可正常显示的数据怎么办

问题排查结果

问题出在checkout.html的模板循环语法错误,具体错误点如下:

  • 循环临时变量和传入的上下文变量重名,且和循环体内调用的变量名不匹配:你写的循环语句是{% for items in items%},循环临时变量为复数items,但循环体中你调用的是单数item对象,同时临时变量和你从视图传入的items列表同名,会覆盖原列表数据,导致无法正常取值。
  • cart.html中循环语句是正确的{% for item in items %},所以能正常渲染数据。

修正方案

将checkout.html中的循环语句修改为如下内容即可:

{% for item in items %}

<div class="cart-row">
    <div style="flex:2"><img class="row-image" src="{{item.product.imageURL}}"></div>
    <div style="flex:2"><p>{{item.product.name}}</p></div>
    <div style="flex:1"><p>${{item.product.price}}</p></div>
    <div style="flex:1"><p>x {{item.quantity}}</p></div>
</div>

{% endfor %}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 12:57:01