Form templates

91 views
Skip to first unread message

Steven Brown

unread,
Apr 24, 2013, 6:58:42 PM4/24/13
to we...@googlegroups.com
I have a few questions, and I'm not even sure how to google for them.  I am writing a page with a form.  The form consists of several identical lines of fields, where the user can fill in as many or as few lines of information as they want.  So, each line is going to have a date field, a dropdown indicating a selected option, and a text box for some notes.  Now, I'm sure I could implement this by building the python-side form array with a loop, where every time through the loop adds another set of fields to the list of fields.  My question is, on the HTML side, how should I access them to render them?  My first instinct is to just use an eval statement; something like

$:for i in range(10): 
    eval('form.date' + i + '.render()')
eval('form.choice' + i + '.render()')
eval('form.notes' + i + '.render()')
print '<br/ >'

But this seems inelegant, not least because everyone has always drilled into me to never
use eval except in the most dire of circumstances. Is there a better way? What would also be
good, is if there was some way I could define how to render a single line, in another place,
and then call that process inside the for loop above.

Another thing I am trying to do in the cleanest, most efficient way possible is change
what's in the dropdown, depending on what is selected in the date field (and later another
dropdown). I found this: http://stackoverflow.com/questions/8763564/how-do-i-update-a-dropdown-menu-when-another-dropdown-menu-option-is-selected

which suggests using jQuery, but I'm wondering first, if there is any way strictly within web.py
to do it. Second, I am going to populate the dropdown from a database, and the only way I can
figure of doing everything, I need to a database query for every set of options. So, am I
correct in that assumption, or is there is a way to minimize dB hits.

Finally, is there a better place to get documentation for web.py? webpy.org seems pretty sparse,
not to mention poorly organized.

Anand Chitipothu

unread,
Apr 25, 2013, 1:02:45 AM4/25/13
to webpy
It is better to extend the Form class and add a get_input method. 

class Form(web.form.Form):
    def get_input(self, name):
        for x in self.inputs:
            if x.name == name: 
                return x

With this you can use form.get_input("date" + str(i)).render()

Anand



--
You received this message because you are subscribed to the Google Groups "web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webpy+un...@googlegroups.com.
To post to this group, send email to we...@googlegroups.com.
Visit this group at http://groups.google.com/group/webpy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Anand
http://anandology.com/

Tomas Schertel

unread,
Apr 25, 2013, 1:04:24 PM4/25/13
to we...@googlegroups.com

To populate dropdown with database values, you can use a jquery plugin.
Two examples:
https://github.com/tuupola/jquery_chained
http://github.e-sites.nl/populate/
 

Jorge Toro

unread,
Apr 28, 2013, 10:21:55 AM4/28/13
to we...@googlegroups.com
2013/4/25 Tomas Schertel <tsch...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webpy+un...@googlegroups.com.
To post to this group, send email to we...@googlegroups.com.
Visit this group at http://groups.google.com/group/webpy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tomas Schertel

unread,
Apr 28, 2013, 3:40:58 PM4/28/13
to we...@googlegroups.com
Nice example Jorge.
I'll try this :)

Steven Brown

unread,
Apr 28, 2013, 4:59:59 PM4/28/13
to we...@googlegroups.com
The first link looks pretty slick, so I'm going to use that; but how do I give the different options a class?  web.py says that each option is created by a tuple with the id and description, but there's nothing I can find about giving them classes.  I would like to avoid adding the class after the form is rendered if possible.
Reply all
Reply to author
Forward
0 new messages