在 Tornado Web 服务器的处理程序中,我有以下代码:
class NetworkSensorsHandler(BaseHandler):
# Requires authentication
@tornado.web.authenticated
@tornado.web.removeslash
def get(self, nid):
# Mandatory argument action = ['view' | 'edit']
# Used only for visualization purposes
action = self.get_argument('action').lower()
if action not in ['view', 'edit']:
raise tornado.web.HTTPError(404, "Unknown action: " + str(action))
# Retrieve the current user
usr = self.get_current_user()
usr_id = usr['id']
self.lock_tables("read", ['nets_permissions as n'])
perm = self.db.get("SELECT n.perm FROM nets_permissions as n \
WHERE n.network_id=%s AND n.user_id=%s", nid, int(usr_id))
self.unlock_tables()
# Check whether the user has access to the network
perms = self.check_network_access(nid, perm['perm'])
self.lock_tables("read", ['devices'])
# Retrieve the sensors
sens = self.db.query("SELECT * FROM devices \
WHERE network_id=%s", nid)
self.unlock_tables()
# get network info
net = self.get_network(nid);
# get the permissions on the network
writeable = self.get_network_access(net.id, PERM_WRITE)
#s['commands'] = sensors_config[net['ntype']][s['type']]['commands']
sens_config = sensors_config[net.ntype]
# Retrieve the current user
net_id = net['id']
# Retrieve the current rights
self.lock_tables("read", ['users as u', 'nets_permissions as m'])
user = self.db.query("SELECT u.id, u.name, n.perm FROM users as u LEFT OUTER JOIN (SELECT * FROM nets_permissions as m WHERE network_id=%s) as n on u.id = n.user_id WHERE (n.perm <> 3 or n.perm is NULL)", int(net_id))
self.unlock_tables()
# Render the networks page
self.render("sensors.html", sensors=sens, perms=perms, net=net, action=action, writeable=writeable, sens_config=sens_config, users=user)
现在在 html 页面中,我有一部分代码是这样的:
{% for sens in sensors %}
.............
{% end %}
但很明显,当我有一些传感器然后我删除它们时,在最后一次传感器删除后我得到这个错误:
UnboundLocalError:分配前引用的局部变量“sens”
因为我认为 Tornado 中的 sens 数组是空的。如果我在数据库表中手动添加一个传感器,页面工作正常!
如果我将 if...end 块放在 {% if sensors %}...{% end %} 块中是相同的,因为页面没有同时识别变量 sens。
如果查询结果 sens 为空,如何排除 if...end 语句中的部分?或者我必须在 Tornado 处理程序中初始化 sens 变量?
非常感谢。请帮我!
编辑
完整的回溯是这样的:
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/tornado/web.py", line 988, in _execute
getattr(self, self.request.method.lower())(*args, **kwargs)
File "/usr/lib/python2.6/site-packages/tornado/web.py", line 1739, in wrapper
return method(self, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/tornado/web.py", line 1096, in wrapper
return method(self, *args, **kwargs)
File "./wsn.py", line 699, in get
self.render("sensors.html", sensors=sens, perms=perms, net=net, action=action, writeable=writeable, sens_config=sens_config, users=user)
File "./wsn.py", line 349, in render
tornado.web.RequestHandler.render(self, *args, **kwargs)
File "/usr/lib/python2.6/site-packages/tornado/web.py", line 474, in render
html = self.render_string(template_name, **kwargs)
File "/usr/lib/python2.6/site-packages/tornado/web.py", line 586, in render_string
return t.generate(**args)
File "/usr/lib/python2.6/site-packages/tornado/template.py", line 253, in generate
return execute()
File "sensors_html.generated.py", line 363, in _execute
_tmp = sens.id # sensors.html:203 (via base.html:152)
UnboundLocalError: local variable 'sens' referenced before assignment