Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:11:31+00:00 2026-06-14T15:11:31+00:00

In an handler of Tornado web server I have this code: class NetworkSensorsHandler(BaseHandler): #

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:11:32+00:00Added an answer on June 14, 2026 at 3:11 pm

    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 %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a simple async case, handler might look like: @tornado.web.authenticated @tornado.web.asynchronous def post(self): AsyncHTTPClient().fetch(http://api.example.com/,
I have this code (hello.py): import os,sys import tornado.ioloop import tornado.web import tornado.httpserver #http
I have a Tornado application structured like below: ... class Application(tornado.web.Application): def __init__(self): ...
I'm trying to unit test my RESTful API. Here's my API: class BaseHandler(tornado.web.RequestHandler): def
I've been playing around a bit with the Tornado web server and have come
I have a Class that I'm instantiating and then passing into a Tornado Web
I have an event handler set up using plain javascript like this: myElement.addEventListener('drop', handleDrop,
I have a basic app set up using Backbone.js and Tornado Web. When I
I have recently been exploring the Tornado web framework to serve a lot of
I have a grammar problem. I have this Tornado statement in my html page:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.