Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get form POST input in Tornado?

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.

like image 476
Jand Avatar asked Sep 05 '15 23:09

Jand


1 Answers

Try code below

self.get_body_argument("alt", default=None, strip=False)
like image 176
DreadfulWeather Avatar answered Sep 25 '22 10:09

DreadfulWeather