Bottlepy/Flask - 如何设置复选框?

And*_*lli 4 python checkbox bottle

如果它在过去的模板中被选中,我正在尝试设置一个复选框。换句话说,如果用户选中复选框并单击提交按钮,他应该能够看到他选中了哪些选项。我的代码是这样的:

if request.GET.get('submit', '').strip():
    checkbox = request.GET.get('box1')
    return template('my_template.j2', box1 = checkbox)
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

mwi*_*.me 7

在您的模板文件中,您可以添加以下内容:

<input type="checkbox" name="box1" value="box1" {{'checked="checked"' if box1 else ""}}/>
Run Code Online (Sandbox Code Playgroud)

您可以在花括号内使用传递到模板中的 python 对象,您可以在有关内联表达式文档中找到更多信息。