-
April 25, 2013 Tornado + WTForms = TypeError: formdata should be a multidict type wrapper ...
If you’re here from some search engine, that means you’ve been working with Tornado and WTForms, and you’ve faced the same error –
(Pdb) form = SomeForm(self.request.arguments) *** TypeError: formdata should be a multidict-type wrapper that supports the 'getlist' methodThe thing is, Tornado does provide a dictionary that contains the data like a MultiDict should do for GET/POST/others request arguments –
{'_xsrf': ['b2c0d0bebbf84516a8aa0462bb41ea0f'], 'password': ['password'], 'email': ['[email protected]']}But it sends it as a dictionary –
(Pdb) type(self.request.arguments) <type 'dict'>And Python dictionaries don’t have a
getlist()method. And WTForms requires the arguments object to have that method.Workaround?
class SimpleMultiDict(dict): def getlist(self, key): return self[key] def __repr__(self): return type(self).__name__ + '(' + dict.__repr__(self) + ')'This simply makes a subclass of
dictand adds thegetlist()method. So, What did we gain?(Pdb) SimpleMultiDict(self.request.arguments) SimpleMultiDict({'_xsrf': ['b2c0d0bebbf84516a8aa0462bb41ea0f'], 'password': ['password'], 'email': ['[email protected]']}) (Pdb) arguments = SimpleMultiDict(self.request.arguments) (Pdb) dir(arguments) ['__class__', (skipping some default methods) , 'clear', 'copy', 'fromkeys', 'get', 'getlist', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'] # Notice the getlist() method (Pdb) arguments.getlist('email') ['[email protected]'] (Pdb) form = LoginForm(arguments) (Pdb) form.email.data '[email protected]'Now there is a point to note here. Tornado sends the arguments data in a simple dictionary that has each of the values inside a list. Here is a pointer if you’re using some framework that sends a simple dictionary, with the arguments not in list, and that also doesn’t have a
getlist()method, just change thegetlist()method to this –class SimpleMultiDict(dict): def getlist(self, key): return self[key] if type(self[key]) == list else [self[key]] def __repr__(self): return type(self).__name__ + '(' + dict.__repr__(self) + ')'This just turns the simgle values to a list, just what
getlist()is supposed to do. - April 24, 2013 To Keep Coming Back
- April 21, 2013 Interactive Shell Access to Tornado Local Environment
- April 15, 2013 Shubho Nababarsha, 1420
- April 14, 2013 From 20,000 feet above ground ..
- February 12, 2013 Moving to Jekyll and Github
- November 23, 2012 Where Do I See Myself in 10 Years?
- September 26, 2012 How to Reach PyCon India 2012
- August 26, 2012 Why You Should Be Bothered About Twitter's t.co Url Shortener And A Way To Bypass It
- August 22, 2012 The Zen of Python
- August 18, 2012 Kolkata Road Map for Durga Puja 2012
- July 2, 2012 Rooting Xperia Neo V MT11i with 4.0.2.A.0.62 Build (on Ubuntu)
- June 27, 2012 PyCon India 2012 - More Details
- June 19, 2012 PyCon India 2012 Registration is Open!
- June 14, 2012 Development and Deployment of Flask app on Nginx with uWSGI+Supervisor
- April 4, 2012 Instagram for Android still needs a lot of work
- April 1, 2012 How aware are you of what's going around you?
- March 30, 2012 My Life, Nowadays.
- March 30, 2012 Education, how I wanted it to be.
- March 11, 2012 My Website's New Homepage
- February 12, 2012 The Most Useless Machine Ever
- February 12, 2012 Making Skype usable on Fedora 14 and maybe later versions too
- January 19, 2012 Interfacing external GSM modem with Android Workbench
- January 19, 2012 Firefox Tilt 3D add-on
- January 18, 2012 The Heart Attack Grill
- January 15, 2012 Painting the Cage
- January 13, 2012 Avail Request Object in Django Form Validation
- January 11, 2012 Setup Bridged Network inside VirtualBox on Debian/Ubuntu
- January 9, 2012 Where The F*ck Should You Go For Drink?
- January 9, 2012 Geek or Nerd? Know The Difference.
- December 18, 2011 PHP Console Inside Your Browser
- November 6, 2011 Manage Your Cloud Server Easily Using Virtualmin
- November 5, 2011 A Quick Tour to World of Programming
- August 28, 2011 WeekEndHack: The New Thing in Town!
- August 18, 2011 First Ever Apple Logo
- August 5, 2011 Yahoo! Open Hack India, 2011 - Memories
- July 28, 2011 All Set for Yahoo! Open Hack India 2011, Bangalore. #openhackindia
- May 16, 2011 Chrome: Instant Desktop Notification of Your Google Reader Feed
- May 15, 2011 Laden is Dead. Is That So? ;)
- May 7, 2011 Only Domain Name Is Not Enough
- May 7, 2011 Modifying MySQL Database using C#.NET
- May 6, 2011 MySQL's Connector for .NET v6.3.6 has bugs it seems
- April 29, 2011 Validating Regular Expression
- April 26, 2011 How Do You Leave Your Home, Again?
- April 1, 2011 Google's +1 Button and My View on it
- April 1, 2011 Facebook's Comment Posting Failure Error Handler
- March 4, 2011 A Day with KWS(Kolkata Weekend Shoots)
- February 26, 2011 Selective/Conditional Update with Yum
- February 25, 2011 Install Adobe Flash Player 10.2 on Fedora 14 x86_64
- February 21, 2011 Comments from Known faces, Unknown faces
- February 6, 2011 And They Called My Name
- February 4, 2011 A Fedora Day at GCECT, Kolkata
- January 20, 2011 PHP Enthusiast? Discover new Functions Right on Your Timeline/wall
- December 31, 2010 Year Ending Kolkata Meetup ’10
- December 30, 2010 Year Ending Meetup - Tweets
- December 24, 2010 Customize Wordpress' Automated Permalink Generation
- December 11, 2010 Startup Saturday Kolkata – Tweets
- December 10, 2010 Been Wondering Why Facebook Doesn't Have a 'Dislike' Button?
- December 6, 2010 The First 50 Tweets Ever Posted
- December 4, 2010 Need a Seamless Background Texture?
- November 17, 2010 Build your own IRC bot in PHP
- October 31, 2010 Auto Refresh your Dabr client
- October 3, 2010 MySQL Database of 1000 Indian City Names
- October 1, 2010 Request or download major Linux distro CDs, absolutely Free.
- September 25, 2010 Return Multiple Values from AJAX script with ease
- August 27, 2010 URL Shortening at your finger tip. :)
- August 22, 2010 Get two Page hits for a single link. ;)