复选框 python 瓶的值
value of checkbox python bottle
我正在尝试检索用户已 select 编辑的复选框的标签(或值)并将它们 return 到另一个页面。此逻辑适用于文本条目、select 选项和收音机,所有这些都包含在相同的 python post 方法中(所以我知道该方法有效)但我无法获取它可以与复选框一起使用。
***如果我通过在 form.tpl 中的输入行末尾插入“checked”将框设置为默认选中,则会列出第一部电影,但如果在结果,然后它抛出一个错误,说第二部电影未定义。
***如果我不设置默认勾选框,即使用户在表单中勾选框并提交,电影也不会出现。
form.tpl:
<body>
<form action="/form/results" class="needs-validated" method="post">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="movie1" name="movie1" value="Kill Bill Vol. II">
<label class="form-check-label" for="movie1">Kill Bill Vol. II</label>
<input type="checkbox" class="form-check-input" id="movie2" name="movie2" value="Fight Club">
<label class="form-check-label" for="movie2">Fight Club</label>
</div>
</form>
</body>
test.py:
@post('/form/results')
def show_results():
movie1 = request.forms.get('movie1')
movie2 = request.forms.get('movie2')
return template('results', movie1=movie1, movie2=movie2)
results.tpl:
<body>
<p>Title(s) of movie(s) chosen: {{movie1}} {{movie2}}</p>
</body>
复选框字段对我来说是通过的,但是作为 "on"
或您提供的值(如果选中)或 None
(如果未选中),这似乎足以推断状态。这是通过每个页面传递选中状态的最小示例:
server.py
from bottle import post, request, route, run, template
@post("/form/results")
def show_results():
movie1 = request.forms.get("movie1")
movie2 = request.forms.get("movie2")
print(repr(movie1), repr(movie2))
return template("form.tpl", movie1=movie1, movie2=movie2)
@route("/form/results")
def items_():
return template("form.tpl", movie1=None, movie2=None)
if __name__ == "__main__":
run(host="localhost", port=8080, debug=True, reloader=True)
form.tpl
<!DOCTYPE html>
<html>
<body>
<form action="/form/results" method="post">
<input
type="checkbox"
name="movie1"
value="Kill Bill Vol. II"
{{"checked" if movie1 else ""}}
>
<label class="form-check-label" for="movie1">Kill Bill Vol. II</label>
<input
type="checkbox"
name="movie2"
value="Fight Club"
{{"checked" if movie2 else ""}}
>
<label class="form-check-label" for="movie2">Fight Club</label>
<input type="submit">
</form>
</body>
</html>
几个请求检查其中一个框后在终端上的输出:
127.0.0.1 - - [18/Mar/2022 12:17:04] "GET /form/results HTTP/1.1" 200 499
127.0.0.1 - - [18/Mar/2022 12:17:06] "GET /sw-debug.js HTTP/1.1" 404 742
None 'Fight Club'
127.0.0.1 - - [18/Mar/2022 12:17:08] "POST /form/results HTTP/1.1" 200 506
127.0.0.1 - - [18/Mar/2022 12:17:10] "GET /sw-debug.js HTTP/1.1" 404 742
'Kill Bill Vol. II' None
127.0.0.1 - - [18/Mar/2022 12:17:17] "POST /form/results HTTP/1.1" 200 506
127.0.0.1 - - [18/Mar/2022 12:17:19] "GET /sw-debug.js HTTP/1.1" 404 742
我正在尝试检索用户已 select 编辑的复选框的标签(或值)并将它们 return 到另一个页面。此逻辑适用于文本条目、select 选项和收音机,所有这些都包含在相同的 python post 方法中(所以我知道该方法有效)但我无法获取它可以与复选框一起使用。
***如果我通过在 form.tpl 中的输入行末尾插入“checked”将框设置为默认选中,则会列出第一部电影,但如果在结果,然后它抛出一个错误,说第二部电影未定义。
***如果我不设置默认勾选框,即使用户在表单中勾选框并提交,电影也不会出现。
form.tpl:
<body>
<form action="/form/results" class="needs-validated" method="post">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="movie1" name="movie1" value="Kill Bill Vol. II">
<label class="form-check-label" for="movie1">Kill Bill Vol. II</label>
<input type="checkbox" class="form-check-input" id="movie2" name="movie2" value="Fight Club">
<label class="form-check-label" for="movie2">Fight Club</label>
</div>
</form>
</body>
test.py:
@post('/form/results')
def show_results():
movie1 = request.forms.get('movie1')
movie2 = request.forms.get('movie2')
return template('results', movie1=movie1, movie2=movie2)
results.tpl:
<body>
<p>Title(s) of movie(s) chosen: {{movie1}} {{movie2}}</p>
</body>
复选框字段对我来说是通过的,但是作为 "on"
或您提供的值(如果选中)或 None
(如果未选中),这似乎足以推断状态。这是通过每个页面传递选中状态的最小示例:
server.py
from bottle import post, request, route, run, template
@post("/form/results")
def show_results():
movie1 = request.forms.get("movie1")
movie2 = request.forms.get("movie2")
print(repr(movie1), repr(movie2))
return template("form.tpl", movie1=movie1, movie2=movie2)
@route("/form/results")
def items_():
return template("form.tpl", movie1=None, movie2=None)
if __name__ == "__main__":
run(host="localhost", port=8080, debug=True, reloader=True)
form.tpl
<!DOCTYPE html>
<html>
<body>
<form action="/form/results" method="post">
<input
type="checkbox"
name="movie1"
value="Kill Bill Vol. II"
{{"checked" if movie1 else ""}}
>
<label class="form-check-label" for="movie1">Kill Bill Vol. II</label>
<input
type="checkbox"
name="movie2"
value="Fight Club"
{{"checked" if movie2 else ""}}
>
<label class="form-check-label" for="movie2">Fight Club</label>
<input type="submit">
</form>
</body>
</html>
几个请求检查其中一个框后在终端上的输出:
127.0.0.1 - - [18/Mar/2022 12:17:04] "GET /form/results HTTP/1.1" 200 499
127.0.0.1 - - [18/Mar/2022 12:17:06] "GET /sw-debug.js HTTP/1.1" 404 742
None 'Fight Club'
127.0.0.1 - - [18/Mar/2022 12:17:08] "POST /form/results HTTP/1.1" 200 506
127.0.0.1 - - [18/Mar/2022 12:17:10] "GET /sw-debug.js HTTP/1.1" 404 742
'Kill Bill Vol. II' None
127.0.0.1 - - [18/Mar/2022 12:17:17] "POST /form/results HTTP/1.1" 200 506
127.0.0.1 - - [18/Mar/2022 12:17:19] "GET /sw-debug.js HTTP/1.1" 404 742