How to get a POST login form in Tornado?

I am new to the Tornado platform and try to make a simple form for uploading 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 get the Submitted file using:

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

However, I cannot get 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'm out of ideas, so appreciate your help.

UPDATE:

I found this to work:

 alt = self.get_argument('alt') 

But still open to better solutions.

+5
source share
1 answer

Try the code below

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

All Articles