tornado URL and HTML form

2019-07-05 05:20发布

问题:

I'm using tornado and I want to Insert something to my MongoDB from values in a HTML form.

in the HTML file I have a form like this:

<form method="get" > 

with 2 textbox and a submit button. and I don't know what to set as "action"

I have a handler class with a function called "post" like bellow:

 class MyHandler(tornado.web.RequestHandler):
  def post(self):
     name  = self.get_argument("Name", "")
     index = self.get_argument("Index","")
      .... code for updating MongoDB

I have a file called BaseUrl.py that contains:

(/admin/edit[/]?',MyHandler  )

but it seems that the "post" function in myHandler does not execute. could you please give me some advice about how to properly set my URLs and form actions?

回答1:

Change the form method to POST as you are handling in a POST request:

<form method="POST" >

You also need to provide an action if the form is served from different page, so your form should be:

<form method="POST" action="/admin/edit">


回答2:

Your post method isn't called because your form specifies method="get". Change that to method="post" and it'll probably work.

If the action is empty the browser will submit the request to the current page, so if you have a get handler serving the form at the same URL you don't need to specify it.