Question

In an handler of Tornado web server I have this code:

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)

Now in the html page I have a part of code like this:

 {% for sens in sensors %}
 .............
 {% end %}

But obviously when I have some sensors and then I delete them, after the last sensor delete I obtain this error:

UnboundLocalError: local variable 'sens' referenced before assignment

because the sens array in Tornado is empty I think. If I add a sensor in the DB table manually, the page works fine!

If I put the if...end block in a {% if sensors %}...{% end %} block is the same because the page didn't recognize at the same time the variable sens.

How can I exclude the part in the if...end statement if the query result sens is empty? Or I have to initialize the sens variable in the Tornado handler?

Thank you very much. Please help me!

EDIT

The complete traceback is this:

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
Was it helpful?

Solution

Can you put the complete HTML please or verify if line 203 in your sensors.html is between your "for" loop.

{% for sens in sensors %}
............. # line 203
{% end %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top