Dynamic form generation using FORM()

121 views
Skip to first unread message

Chris Hepworth

unread,
Jan 14, 2014, 4:44:49 PM1/14/14
to web...@googlegroups.com
I am trying to make a custom form which allows the selection of a set of images that are in the database. So far I have the following code:

rows = db().select(db.a_table.name,db.a_table.image)
    fields
= []
   
for row in rows:
        fields
+= [
        INPUT
(_type="checkbox", _value=row.image) , row.f_name
       
]
    form
=FORM(fields,INPUT(_type='submit'))

I know for sure that the database query is correct and is not a problem at all now. However, when I insert the form into the view it appears as follows:

[<gluon.html.INPUT object at 0xaf12bc2c>, 'Koi', <gluon.html.INPUT object at 0xaf12bb8c>, 'Goldfish'...

The only thing that renders as intended is the submit button. Am I going about this problem the right way? And if not, is there a way to achieve the effect I want?

Anthony

unread,
Jan 14, 2014, 7:23:23 PM1/14/14
to web...@googlegroups.com
If you want to submit a list of inputs to FORM(), then that's all you can submit. So, append the submit input to the fields list and then just do FORM(fields).

Also, if db.a_table.image is an upload field, it will just store the transformed filename, which won't be very informative to the users. You'll probably want to show the original filename (it may be easiest to store that in a separate field rather than extract it from the transformed filename).

Anthony

Chris Hepworth

unread,
Jan 14, 2014, 11:10:54 PM1/14/14
to web...@googlegroups.com
You know, I hate to say it but that never even occurred to me to try. I got so distracted with other things that I never took the time to really evaluate what could be happeing. Thank you, Anthony.

Tim Richardson

unread,
Jan 15, 2014, 8:08:24 AM1/15/14
to web...@googlegroups.com
out of curiosity why not use SQLFORM.grid with selectable?

Chris Hepworth

unread,
Jan 15, 2014, 12:13:02 PM1/15/14
to web...@googlegroups.com
I actually have a very odd data set that is getting put into the form. They represent several different tables and the only real unifying feature is the uploaded photos. The idea is to fetch a list of photos to retrieve for the user using a single form. That is why the checkbox value is the encrypted name of the file and the assigned name is displayed next to them.


On Wed, Jan 15, 2014 at 6:08 AM, Tim Richardson <t...@growthpath.com.au> wrote:
out of curiosity why not use SQLFORM.grid with selectable?

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/lRbZyCV70z4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages