Search code examples
pythontornado

Python/Tornado – Why am I getting the error: self is not defined?


I just started using Tornado. All I would like to do is click the submit button on index and be sent to login via the form action (this works so far); however, I am having trouble getting the posted data to login and making it work. First question is, why am I getting the error:

 password = self.get_argument('password', None)
 NameError: name 'self' is not defined

Here's the python so far:

 import os

 import tornado.web
 import tornado.ioloop

 class MainHandler(tornado.web.RequestHandler):
      def get(self):
          self.render("index.html")

 class LoginHandler(tornado.web.RequestHandler):
      def post(self):
          useremail = self.get_argument('useremail', None)
          password = self.get_argument('password', None)
          self.render("login.html") 

 def main():
     application = tornado.web.Application([ 
          (r"/", MainHandler),
          (r"/login", LoginHandler), 
          (r"/css/(.*)", tornado.web.StaticFileHandler, {"path": os.path.join(os. path.dirname(__file__), 'css')}),
          (r"/pictures/(.*)", tornado.web.StaticFileHandler, {"path": os.path.join(os.path.dirname(__file__), 'pictures')}),
     ])
     application.listen(8888)
     tornado.ioloop.IOLoop.instance().start()

 if __name__ == "__main__":
     main()

And secondly, is there anything else that looks glaringly wrong here? I am very uncertain as to how I'm supposed to render webpages like Login.html vs. Index.html, etc.

Syntactically, this seems to work also: return self.write(open("index.html", 'r').read()) What exactly does that do versus what I currently have?

Thanks for the help!


Solution

  • Your indentation is goofed, mixing spaces and tabs. Use python -tt to verify.