Problem collecting POST data using a WTForms form

533 views
Skip to first unread message

pgoetz

unread,
Apr 19, 2013, 2:38:31 PM4/19/13
to bott...@googlegroups.com
I'm trying to use WTForms with limited success, and am wondering if anyone knows how to pass request.forms.POST data to a WTForms form.

Sending a blank form works fine:

@rolodex.get('/add_new')
def new_entry_form():
    form = RolodexEntry()
    return bottle.template('entry_form2',form=form)

However, when I try and stuff the posted data back into a form, I get an internal server error:

@rolodex.post('/add_entry')
def add_entry():
    new_entry = RolodexEntry(request.forms.POST)
    ...

This would be easier to debug if I knew what format the WTForms form is expecting formdata to be in (question posted on the WTForms list), but meanwhile I'm curious to know if anyone has gotten this working.

Iuri

unread,
Apr 19, 2013, 2:41:48 PM4/19/13
to bott...@googlegroups.com
Please, enable bottle debug mode and copy/paste the error here.

bottle.debug(True)

thanks!




--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.
 
---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

pgoetz

unread,
Apr 19, 2013, 2:59:38 PM4/19/13
to bott...@googlegroups.com
Hi Luri -

Sadly, I have bottle running in debug mode already, but the error message I get doesn't tell me anything.  Here is the actual route (if I comment on the new_entry = line, I get Success!!!!, as expected)


@rolodex.post('/add_entry')
def add_entry():
    new_entry = RolodexEntry(request.forms.POST)
#    return(" ".join([field.name for field in new_entry]))
    return("Success!!!!")


Here is the the resulting error message:

Error: 500 Internal Server Error

Sorry, the requested URL 'http://127.0.0.1:8080/add_entry' caused an error:

Internal Server Error

Iuri

unread,
Apr 19, 2013, 3:08:45 PM4/19/13
to bott...@googlegroups.com
It is difficult to help without information.

This issue https://github.com/defnull/bottle/issues/250 has some WTForms code.


--

pgoetz

unread,
Apr 19, 2013, 3:24:41 PM4/19/13
to bott...@googlegroups.com
I understand; I'm trying to look at the source code now.  I tried both of the different syntax examples in the link you provided below, and still get the same error message.  I guess I should have also mentioned that I'm using bottle 0.11.6.

This is the relevant code from WTForms form.py file.  I only have the 0.12 bottle source code, but it looks like bottle supports the getlist method via getall:

getlist = getall


    def process(self, formdata=None, obj=None, **kwargs):
        if formdata is not None and not hasattr(formdata, 'getlist'):
            if hasattr(formdata, 'getall'):
                formdata = WebobInputWrapper(formdata)
            else:
                raise TypeError("formdata should be a multidict-type wrapper that supports the 'getlist' method")

        for name, field, in iteritems(self._fields):
            if obj is not None and hasattr(obj, name):
                field.process(formdata, getattr(obj, name))
            elif name in kwargs:
                field.process(formdata, kwargs[name])
            else:
                field.process(formdata)

Sathit Jittanupat

unread,
Apr 21, 2013, 11:37:02 AM4/21/13
to bott...@googlegroups.com
I 've never use WTForms, but notice..

1.     new_entry = RolodexEntry(request.forms.POST)
 why not 
request.POST.decode() or request.forms.decode().


2. request.forms.POST is FormsDict instance not a dict,
If WTForms requires a dict instance this may cause error.  
use dict(request.POST)



2013/4/20 pgoetz <pgo...@gmail.com>

pgoetz

unread,
Apr 22, 2013, 4:36:34 PM4/22/13
to bott...@googlegroups.com
Thanks!  That worked.  I had the wrong version of the documentation open again (0.10), which didn't include the comment about how to make WTForms work as per the link you provided.  For other people reading this, this POST processing route works:

@rolodex.post('/add_entry')
def add_entry():
    new_entry = RolodexEntry(bottle.request.forms.decode())
    myfields = ""
    for field in new_entry:
        myfields += "<p>" + field.name + ": " + field.data + "</p>\n"
    return(myfields)
Reply all
Reply to author
Forward
0 new messages