2 forms, 1 page, 1 handler?

106 views
Skip to first unread message

Matthew

unread,
Aug 4, 2010, 6:14:13 PM8/4/10
to tipfy
I haven't been able to find an example of using multiple forms on one
page (or one Handler) for tipfy. I'm trying to allow users to both
answer (form) and comment on an item (another form) from the same
page. What's the best way to accomplish this in tipfy?

Rodrigo Moraes

unread,
Aug 5, 2010, 9:29:26 AM8/5/10
to tipfy
Hi,
IMO the best is to set a hidden field in each form, then check for it
to decide which one to process. Not different than what you do
normally in any framework, I think. Does this answer the question?

-- rodrigo

Matthew

unread,
Aug 12, 2010, 5:46:56 PM8/12/10
to tipfy
Thank you, Rodrigo. Here is a PHP reference for the method you
suggest, http://bavotasan.com/tutorials/processing-multiple-forms-on-one-page-with-php/,
however I'm not sure of the equivalent check for 'array_key_exists' in
tipfy. Is it an item from kwargs?

BTW, I apologize for the late reply. I've been out of town for the
past week.

Rodrigo Moraes

unread,
Aug 12, 2010, 6:42:59 PM8/12/10
to tipfy
On Aug 12, 6:46 pm, Matthew wrote:
> Thank you, Rodrigo. Here is a PHP reference for the method you
> suggest,http://bavotasan.com/tutorials/processing-multiple-forms-on-one-page-...,
> however I'm not sure of the equivalent check for 'array_key_exists' in
> tipfy. Is it an item from kwargs?

Hi,
Here's a quick example with 2 forms in a page. See that both have a
"process" hidden field that identifies what to process. First the
HTML:

<form method="post" action="/">
<input type="hidden" name="process" value="save_user">
<input type="text" name="username">
<input type="submit" name="submit" value="save">
</form>

<form method="post" action="/">
<input type="hidden" name="process" value="save_issue">
<input type="text" name="description">
<input type="submit" name="submit" value="save">
</form>

Then, in the handler:

class MyForm(RequestHandler):
def get(self, **kwarg):
# ... display the forms.

def post(self, **kwargs):
# What to process?
process = self.request.form.get('process')

if process == 'save_user':
# Process user form.
username = self.request.form.get('username')
# ...

elif process == 'save_issue':
# Process issue form.
description = self.request.form.get('description')
# ...

easy, isn't it?
-- rodrigo

Matthew

unread,
Aug 12, 2010, 7:56:42 PM8/12/10
to tipfy
It is now that you've provided some insight into the Request's form,
thank you!

It didn't look as though the Werkzeug API was very complete, or at
least the complete docs were not easy to find
http://werkzeug.pocoo.org/documentation/0.5.1/wrappers.html#werkzeug.Request
Is there a spot on the site that gets more detailed like Kay's
documentation? http://kay-docs.shehas.net/request_response.html

Thanks again for all your help,
Matthew

Rodrigo Moraes

unread,
Aug 12, 2010, 8:35:39 PM8/12/10
to tipfy
On Aug 12, 8:56 pm, Matthew wrote:
> It is now that you've provided some insight into the Request's form,
> Is there a spot on the site that gets more detailed like Kay's
> documentation?http://kay-docs.shehas.net/request_response.html

See these:
Request: http://www.tipfy.org/wiki/guide/request/
Response: http://www.tipfy.org/wiki/guide/response/

They are a compilation of werkzeug docs in a more practical
perspective (I think).

Hope this helps.
-- rodrigo

Stephen Gornick

unread,
Oct 5, 2010, 2:07:24 AM10/5/10
to tipfy
I've a similar type of problem ... can I use one form, two submits:

<form method="post" action="/">
<input type="text" name="username">
<input type="submit" name="submitUser" value="save">

<input type="text" name="description">
<input type="submit" name="submitIssue" value="save">
</form>

haibin

unread,
Oct 5, 2010, 4:28:17 AM10/5/10
to tipfy
Yes that's possible. In your handler you have to test for submitUser
or submitIssue

Stephen Gornick

unread,
Oct 5, 2010, 11:41:33 AM10/5/10
to tipfy
Thanks, I see. I should have added that I'm using wtforms and was
trying to figure out how to see the submit in my handler's
self.form.data, and then realized that to do that I just needed to add
them to my form:

class MyForm(Form):
username = fields.TextField()
description = fields.TextField()
submitUser = fields.SubmitField()
submitIssue = fields.SubmitField()

then self.form.submitUser and self.form.submitIssue each give me a
boolean indicating which one was the one used.
Reply all
Reply to author
Forward
0 new messages