在龙卷风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'
因为龙卷风中的SENS数组是空的。如果我手动在DB表中添加传感器,则页面正常工作!
如果我将if ... end block放在{%if sensors%} ... {%end%}块中的eNd块是相同的P>
如果查询结果sens为空,我如何将零件排除在if ... end语句中?或者我必须在龙卷风处理程序中初始化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
您可以将完整的HTML放置或验证传感器中的第203行。
{% for sens in sensors %}
............. # line 203
{% end %}