How to get form POST input in Tornado?

Tags: , ,



I amd new to Tornado framework and trying to make a simple form to upload images:

<form method="post" action="/uploads/{{uid}}/" enctype="multipart/form-data"  >
    <input type="file" name="file1" /> <br />
  Image info: <input type="text" name="alt" /> <br />
<input class="button" type="submit" value="Upload" class="button" /> 
</form>

I can successfully receive the Posted file using:

if 'file1' in self.request.files:
            if self.request.files['imgfile'][0]: 
                    file1 = self.request.files['imgfile'][0] 

However I’m unable to receive the alt input. I tried alt = self.request.alt but I get this error

AttributeError: 'HTTPServerRequest' object has no attribute 'alt'

and when I use alt = self.request.files['alt'], I get:

  KeyError: 'alt'

I ran out of ideas so appreciate your help.

UPDATE:

I found that this works:

alt = self.get_argument('alt')

But still open for better solutions.

Answer

Try code below

self.get_body_argument("alt", default=None, strip=False)


Source: stackoverflow