Using checkbox widget

9 views
Skip to first unread message

OriginalBrownster

unread,
Jul 31, 2006, 4:18:53 PM7/31/06
to TurboGears
Hi

I want to know how to implement the following source code and template
sample they gave within the widget browser.


I reviewed over the checkbox widget, and I think it will be easier
using the widget than creating my own form in my template.

I am assuming this is supposed to go in the controller


class CheckBoxListDesc(CoreWD):
name = "CheckBox List"
for_widget = CheckBoxList("your_checkbox_list",
options=[(1, "Python"),
(2, "Java"),
(3, "Pascal"),
(4, "Ruby")],
default=[1,4])

and this in the template
<ul xmlns:py="http://purl.org/kid/ns#"
class="${field_class}"
id="${field_id}"
py:attrs="list_attrs"
>
<li py:for="value, desc, attrs in options">
<input type="checkbox"
name="${name}"
id="${field_id}_${value}"
value="${value}"
py:attrs="attrs"
/>
<label for="${field_id}_${value}" py:content="desc" />
</li>
</ul>


However I get a traceback error

Traceback (most recent call last):
File "/var/projects/document_site/se/start-se.py", line 27, in ?
from se.controllers import Root
File "/var/projects/document_site/se/se/controllers.py", line 75, in
?
class CheckBoxListDesc(CoreWD):
NameError: name 'CoreWD' is not defined

I need alot of help with this. I've been trying to use checkboxs within
my application but I have had NO SUCCESS. If someone is willing to help
me out it would be greatly appreciated.

Thank you
Stephen

Michele Cella

unread,
Jul 31, 2006, 5:45:00 PM7/31/06
to TurboGears

Hi Stephen,

I will show you how to build a complete form containing even a
CheckBoxList, in your controller do something like this:

from turbogears import expose, validate, error_handler, flash, widgets,
validators

class MyFields(widgets.WidgetsList):
name = TextField()
email = TextField(validator=validators.Email())
language = CheckBoxList(options=[(1, "Python"), (2, "Java"), (3,


"Pascal"), (4, "Ruby")],
default=[1,4])

my_form = widgets.TableForm(fields=MyFields(), action="save")

now you need two methods methods attached to your controller in this
way:

@expose(template="test.new")
def new(self, tg_errors=None):
if tg_errors is not None:
flash("Please fix all errors")
return dict(my_form=my_form)

@expose()
@validate(form=my_form)
@error_handler(new)
def save(self, name, email, language):
#the data your received is valid and you can add it to the database
...

Hope this helps, keep in mind that when you look at a widget using the
widget browser the source code refers to the code running that example,
generally what you really need is the for_widget portion, the template
tab refers to the widget's template for further customization that
oftten is not needed.

Inside the turbogears's trac you can find a complete package that shows
how to use widgets (very easy example anyway), I have updated it to
work with the latest turbogears version (0.9a8), you can find it here:

http://trac.turbogears.org/turbogears/attachment/wiki/SimpleWidgetForm/FormsTutorial-0.9a8.tar.bz2?format=raw

Ciao
Michele

Nicky Ayoub

unread,
Jul 31, 2006, 6:05:23 PM7/31/06
to turbo...@googlegroups.com
The 'class CheckBoxListDesc(CoreWD)' is only relevent to the toolbox. If you are creating you own widgets to be packaged
as an add-on to TG, then it's used to show how your widget is to be used. Basically, it doesn't need to be in your
controller at all. I am using the svn version of TG and I don't recall what version  you are using.
In any case I think you also need a form too. This provides the submit mechanism. I am attaching a very crude example of
what you can use as a base.

In controllers.py  my additions are in bold:

 from turbogears import controllers, expose, validate, redirect, flash
 from turbogears.widgets import CheckBoxList, TableForm

 from cb import json

 log = logging.getLogger("cb.controllers")


for_widget = CheckBoxList("your_checkbox_list",
                              options=[(1, "Python"),
                                       (2, "Java"),
                                       (3, "Pascal"),
                                       (4, "Ruby")],
                              default=[1,4])

S
ome_form = TableForm(

    name='form', submit_text = 'Update',
    fields = [for_widget])


  class Root(controllers.RootController):
     @expose(template="cb.templates.welcome")
     def index(self , **kw):
          import time
          log.debug("Happy TurboGears Controller Responding For Duty")
          if kw:
               flash('FLASH: '+ str(kw))
         return dict(now=time.ctime(), form=Some_form)

In welcome.kid I added ${form.display()}
When you use the submit button, the FLASH message appears at the top of the page.
I hope this little example helps.

Nicky
--
--
Nicky Ayoub
G-Mail Account
cb.tar.gz

Nicky Ayoub

unread,
Jul 31, 2006, 6:07:51 PM7/31/06
to turbo...@googlegroups.com
Looking at Michele's example, I think it is a better place to start than mine... :-(

my_form = widgets.TableForm (fields=MyFields(), action="save")


now you need two methods methods attached to your controller in this
way:

@expose(template="test.new")
def new(self, tg_errors=None):
    if tg_errors is not None:
        flash("Please fix all errors")
    return dict(my_form=my_form)

@expose()
@validate(form=my_form)
@error_handler(new)
def save(self, name, email, language):
    #the data your received is valid and you can add it to the database
    ...

Hope this helps, keep in mind that when you look at a widget using the
widget browser the source code refers to the code running that example,
generally what you really need is the for_widget portion, the template
tab refers to the widget's template for further customization that
oftten is not needed.

Inside the turbogears's trac you can find a complete package that shows
how to use widgets (very easy example anyway), I have updated it to
work with the latest turbogears version (0.9a8), you can find it here:

http://trac.turbogears.org/turbogears/attachment/wiki/SimpleWidgetForm/FormsTutorial-0.9a8.tar.bz2?format=raw

Ciao
Michele


Roger Demetrescu

unread,
Jul 31, 2006, 6:11:57 PM7/31/06
to turbo...@googlegroups.com
Hmmm... regarding to CoreWD, take a look at this thread:

http://groups.google.com/group/turbogears/browse_thread/thread/35aefef01b4e1975/3fb7275ac2a60501

(and forgive me for those stupid misspelling mistakes)


Just to answer your question right now: you don't need to write any
WidgetDescription or CoreWD (which is a WidgetDescriptio btw)... These
2 classes are designed to show a sample of the widget (plus other
information) in the Toolbox Widget Browser.

I'd suggest the reading of this tutorial (try changing
"widgets.TextField" to "widgets.CheckBox") :
http://trac.turbogears.org/turbogears/wiki/SimpleWidgetForm


I hope it will help you. If no, just tell us... :)


Cheers,

Roger

PS: Maybe these videos from
http://www.turbogears.org/docs/devcasts.html can help you too...


On 7/31/06, OriginalBrownster <original...@gmail.com> wrote:
>

OriginalBrownster

unread,
Jul 31, 2006, 9:41:26 PM7/31/06
to TurboGears
I thought both methods really displayed how to use a widget. Thank you
for your help. It is immensely useful.

I will be trying to use these widgets to allow users to select files to
download from my database.

Contructing my own form and using simple checkboxs was causing me too
much difficulty

Thank you

OriginalBrownster

unread,
Aug 1, 2006, 10:37:51 AM8/1/06
to TurboGears
To Nicky Ayoub and Michelle

I implented both your examples and found both to help me understand how
to use the wiget form.

my question for Nicky and perhaps Michelle also is: instead of using
**kw in Nicky's example to bring in what elements have been checked is
there another way to do so?

Also I fully do not understand the function of **kw.


For michelle example I'm sure this is done through the language
parameter in

def save(self, name, email, language):

however I didn't know how to user language to find out which had been
checked off.

Sorry for the inconvience, but I'm really new to turbogears widgets.

Thank you

Reply all
Reply to author
Forward
0 new messages