【发布时间】:2018-12-17 20:56:58
【问题描述】:
x.py 文件:
from bottle import request, template,route,run,post
@route('/')
def index():
return template('val.html')
@post('/result')
def result():
result=request.forms
print(result) #Unable to print
return template("result",result = result)
if __name__ == '__main__':
run(host='localhost',port=8080,debug='True',reloader='True')
val.html 文件:
<!DOCTYPE html>
<html>
<body>
<form action="http://localhost:8080/result" method = "POST">
Select a time:
<input type="time" name="usr_time">
<br> <br>
<input type="checkbox" name="A" value="A is on" >A </input>
<br>
<input type="checkbox" name="B" value="B is on" >B </input>
<br>
<input type="checkbox" name="C" value="C is on" >C </input>
<br><br>
<input type="submit"> </input>
</form>
</body>
</html>
result.html 文件:
<!doctype html>
<html>
<body>
<table border = 1>
{% for key, value in result.items() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</table>
</body>
</html>
html文件放在views文件夹中。
1) 我正在尝试显示用户单击的按钮,但出现错误 - “NameError: name 'key' is not definedUnable to display”。
2) 另外,我无法打印结果。如果我使用result= request.form 然后print(result),这在烧瓶上工作正常。这会在烧瓶上打印字典。但不能使用瓶子。当我使用type(result) 时,它说=<class 'bottle.FormsDict'>。请帮忙。
【问题讨论】:
-
你是否在结束
}之前错过了你的结局%? -
对不起,我已经编辑过了。仍然得到同样的错误
标签: python python-3.x python-requests bottle