【问题标题】:"NameError: name 'key' is not definedUnable to display" error using bottle“名称错误:名称‘键’未定义无法显示”错误使用瓶
【发布时间】: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) 时,它说=&lt;class 'bottle.FormsDict'&gt;。请帮忙。

【问题讨论】:

  • 你是否在结束}之前错过了你的结局%
  • 对不起,我已经编辑过了。仍然得到同样的错误

标签: python python-3.x python-requests bottle


【解决方案1】:

根据https://bottlepy.org/docs/0.11/stpl.html,“% 字符只有在它是一行中的第一个非空白字符时才会被识别。”
所以应该写成下面这样,以保持python模式:

% for key, value in result.items()
...
% end

【讨论】:

  • 其实就是这个。知道了。 result.items():请编辑您的答案,以便我将其标记为正确
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 2015-01-28
  • 1970-01-01
  • 2021-04-02
相关资源
最近更新 更多