【发布时间】:2013-12-06 04:44:16
【问题描述】:
我想使用复选框显示用户想要在购物车中购买的一些商品。
在待售物品的 db.Model 类中,我已包括:
amount = db.StringProperty(required = True)
price = db.StringProperty(required = True)
checked = db.BooleanProperty(default = False)
db.Model 类 html:
<tr>
<td class = "checkbox">
<input type = "checkbox" name = "check">
{{s.checked}}
</td>
<td class = "entry_amount" name = "entry">
{{s.amount}}
</td>
<td class = "entry_price" name = "entry">
{{s.price}}
</td>
</tr>
每次用户访问购买页面时,每个项目的选中属性都设置为 False。在购买页面的 post 方法上,当用户点击提交时,我有以下内容; check 是复选框的名称;该复选框未在 html 中分配值
sells = SellModel.all()
boxcount = 0
for sell in sells:
check = self.request.get('check')
if check:
sell.checked = True
sell.put()
boxcount += 1
if boxcount == 0:
error = "check at least one box"
self.render("buy.html", error = error, sells = sells)
else:
self.redirect('/cart')
buy.html 包括:
<tr class = "table_label">
<th></th>
<th>amount of mp</th>
<th>price per mp</th>
</tr>
{% for sell in sells %}
{{ sell.render() | safe }}
</input>
{% endfor %}
self.redirect 指向购物车页面,其中 get 方法有
cart = SellModel.all()
cart.filter("checked = ", True)
self.render("newbuy.html", cart = cart)
当我进入购物车页面时,每一个列出的待售商品都会被选中,而不仅仅是选中复选框的那些。为什么会这样?
帮助。
【问题讨论】:
标签: python google-app-engine web-applications checkbox jinja2