Python &引用;名称错误:名称';键';不可定义为“可显示”;使用瓶子时出错
x、 py文件:Python &引用;名称错误:名称';键';不可定义为“可显示”;使用瓶子时出错,python,python-3.x,python-requests,bottle,Python,Python 3.x,Python Requests,Bottle,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",re
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>
<!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>
选择一个时间:
A.
B
C
result.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>
<!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>
{键的%result.items()中的值%}
{{key}}
{{value}}
{%endfor%}
html文件放在“视图”文件夹中
1) 我试图显示用户单击的按钮,但我得到了一个错误-“NameError:name'key'未定义为可显示”
2) 此外,我无法打印结果。如果我使用,result=request.form
然后print(result)
,这在烧瓶上效果很好。这把字典印在烧瓶上。但用瓶子是不行的。当我使用type(result)
时,它说=
。请提供帮助。根据“只有当%字符是行中的第一个非空白字符时,才能识别它。”因此,为了保持python模式,应该按照以下方式编写:
% for key, value in result.items()
...
% end
您是否缺少结束前的结尾
%
。还是有同样的错误,实际上是这个。知道了。result.items():请编辑您的答案,以便我将其标记为正确