2 回答
TA贡献1883条经验 获得超3个赞
您在模板中使用了错误的变量名称。它应该是 display_cart 而不是 dict_of_phones。见下文:
{% for phone in display_cart %}
<tr>
<td>{{phone.model}}</td>
<td>{{phone.year}}</td>
<td>${{ "%.2f" % phone.price}}</td>
<td>${{ "%.2f" % phone.price}}</td>
</tr>
{% endfor %}
TA贡献1810条经验 获得超5个赞
我会将电话列表传递到您的模板中,而不是电话字典。此外,您dict_of_phones就是只设置到您的手机项目的最后一个值,因为你覆盖了每一次它的价值dict_of_phones = phone。所以dict_of_phones实际上只是items 中最后一个项目给出的单个电话项目:phone = get_phone_by_id(item)。也许你可以修改你的代码来创建一个电话列表?然后将此列表传递到您的jinja2模板中,大致如下:
@phones.route("/cartt")
def shopping_cart():
total_price = 0
if "cart" not in session:
flash("There is nothing in your cart.")
return render_template("phones/cart.html", display_cart = {}, total = 0)
else:
# Assuming items is correct, looks off
items = [j for i in session["cart"] for j in i]
phones = []
for item in items:
phone = get_phone_by_id(item)
print(phone.id)
# assuming phone has id,model,year, and price attributes
phones.append[phone]
# note total_price of your cart not currently being used in your template
total_price += phone.price
return render_template('phones/cart.html', display_cart=phones, total = total_price)
然后在您的模板中,您可以执行以下操作:
{% for phone in display_cart %}
<tr>
<td>{{phone.model}}</td>
<td>{{phone.year}}</td>
<td>${{ "%.2f" % phone.price}}</td>
<td>${{ "%.2f" % phone.price}}</td>
</tr>
{% endfor %}
希望这有帮助!
添加回答
举报