Search code examples
pythontornado

Python Tornado - Problem accessing value from Form input in HTML


I have a problem accessing the value from a Form input in HTML. I'm using Tornado.

It gives me the error: WARNING:tornado.access:404 GET /Python_Tornado_IV.py?input1=pedro (127.0.0.1) 0.00ms

These are the files:

File "Python_Tornado_IV.py":

import tornado.web
import tornado.ioloop

class MainHandler(tornado.web.RequestHandler):
  def get(self):
    self.render("index.html")
    if self.get_argument("input1") is not None:
      valor = self.get_argument("input1")
      print("Valor introduzido:", valor)
    else:
     print("Não foi introduzido nenhum valor!")

app = tornado.web.Application([(r"/", MainHandler)])
app.listen(8888)
tornado.ioloop.IOLoop.current().start()

File "index.html":

<!Doctype html>
<html>
  <body>
    <form name="form1" action="Python_Tornado_IV.py" method="get">
      <input type="text" name="input1">
      <input type="submit" value="Submit">
    </form>
  </body>
</html>

Both files are in the same directory.


Solution

  • In Tornado, you don't make requests to a file. Rather, you make requests to the registered urls of a handler.

    As you've set the path for your MainHandler as r"/", so this is where you're supposed to make the requests to.

    Change the action of your form to this:

    action="/"