python - 使用谷歌应用程序引擎和Python,如何检查复选框是否被标记?

标签 python google-app-engine web-applications checkbox jinja2

我想使用复选框显示用户想要在购物车中购买的一些商品。

在待售商品的 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/

相关文章:

python - 将颜色条作为图例添加到 matplotlib 散点图(多个子图,多个散点图)

Docker 容器中的 Python 服务器脚本不接收来自主机操作系统的请求

python - GAE用户服务为用户昵称属性返回xml

python - 从 web.py url 访问文件

google-apps-script - 替换已弃用的 UiApp 和 UiInstance

python - django-tables2 为不同的行指定不同的属性

python - 为什么不能将 Deferred 传递给 Python Twisted 中的回调?

google-app-engine - 如何从 GeoPt 属性中提取纬度和经度?

python - 无法访问 Google AppEngine 外部库

css - 如何将字体添加到 Web 应用程序 .css 文件