我想使用复选框显示用户想要在购物车中购买的一些商品。
在待售商品的 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>
每次用户访问购买页面时,每个商品的checked 属性都会设置为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)
当我进入购物车页面时,列出的每件待售商品都会被选中,而不仅仅是选中复选框的商品。为什么会发生这种情况?
帮助。
最佳答案
尝试使用 has_key() python 函数检查字典是否确实具有该键:
check = self.request.has_key('check') #you may also use ('check' in self.request) - see my comment below.
if check:
sell.checked = True
sell.put()
boxcount += 1
以上应该有效。如果没有,请更改您的复选框标记,以便它实际上具有一些值(value):
<input type = "checkbox" name = "check" value = "true">
html 复选框的标准行为是仅在选中复选框时才发布值。请参阅this链接以获取更多信息。
关于python - 使用谷歌应用程序引擎和Python,如何检查复选框是否被标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20416138/