A Couple of Widget Questions

1 view
Skip to first unread message

Kevin Monceaux

unread,
Nov 10, 2006, 1:47:57 PM11/10/06
to TurboGears
Fellow TurboGears enthusiasts,

Okay, I've said it before but I just have to say it again -- the more I dig
into TurboGears the more I'm liking it.

I just finished the "Introduction to Widgets" tutorial at:

http://Docs.TurboGears.org/1.0/IntroductionToWidgets

I found the "Simple Widget Form Tutorial" at:

http://Trac.TurboGears.org/turbogears/wiki/SimpleWidgetForm

It looks like it's geared towards TurboGears 0.9x. Is it still valid for
1.x? Or, is there a 1.x version floating around somewhere. I think I'm
running 1.0b1.

Does the Form Widget have a function to set the focus to the first form
field? I usually do that via a little simple JavaScript along the lines of:

function FieldFocus() {
if (document.forms.length > 0) {
document.forms[0].elements[0].focus();
}
}

Called via the body's onLoad attribute it'll set the field focus to the
first form fild it's finds on a page or do nothing if there's no form on the
page. If Widgets has something built in to do it I'd rather do it via
Widgets. If not, the above has served me well over the years.

Also, does Widgets have any type of input mask functions? For example, for
a phone number field I prefer to enter just the digits, such as:

2545551212

and have the separator characters filled in automatically, transforming it
into:

(254)555-1212

It also comes in handy for things such as social security numbers, dates,
etc. Again, I have a bit of JavaScript I use to accomplish the above but if
it's already in Widgets I don't want to repeat myself there either.


P.S. I can hardly wait for the book!! Has anyone mentioned to Amazon.com
that it's been released? They're still showing that it hasn't been released
yet.


Kevin
http://www.RawFedDogs.net
http://www.WacoAgilityGroup.org
Bruceville, TX

Si hoc legere scis nimium eruditionis habes.

Karl Guertin

unread,
Nov 10, 2006, 2:19:11 PM11/10/06
to turbo...@googlegroups.com
On 11/10/06, Kevin Monceaux <Ke...@rawfeddogs.net> wrote:
> It looks like it's geared towards TurboGears 0.9x. Is it still valid for
> 1.x? Or, is there a 1.x version floating around somewhere. I think I'm
> running 1.0b1.

*most* 0.9 stuff will work with 1.0b, the tutorial looks valid from my
glance at it.

> Does the Form Widget have a function to set the focus to the first form
> field?

I don't believe so.

connect(window,'onload',function() {


if (document.forms.length > 0) {
document.forms[0].elements[0].focus();
}

});

is the fancy way to do it if you're doing MochiKit.


> Also, does Widgets have any type of input mask functions?

I don't think there's anything specific. Check the formencode
validators list. If not, it's pretty easy to write your own validator
that does what you want.

Mark Ramm

unread,
Nov 10, 2006, 10:17:14 PM11/10/06
to turbo...@googlegroups.com
> Does the Form Widget have a function to set the focus to the first form
> field

> connect(window,'onload',function() {


> if (document.forms.length > 0) {
> document.forms[0].elements[0].focus();
> }
> });
>

You could grab the template for the TableForm widget, add this little
snippit to the top of it and overide the template param of TableForm,
and and get what you want.

If you need this kind of form a lot, create your own CustomFormWidget
class, and set it up with it's own default template param.

--Mark Ramm

Jorge Godoy

unread,
Nov 11, 2006, 4:27:00 AM11/11/06
to turbo...@googlegroups.com
"Mark Ramm" <mark.mch...@gmail.com> writes:

Or put this on your master template...

--
Jorge Godoy <jgo...@gmail.com>

Mark Ramm

unread,
Nov 11, 2006, 10:54:58 AM11/11/06
to turbo...@googlegroups.com
> > You could grab the template for the TableForm widget, add this little
> > snippit to the top of it and overide the template param of TableForm,
> > and and get what you want.
> >
> > If you need this kind of form a lot, create your own CustomFormWidget
> > class, and set it up with it's own default template param.
>
> Or put this on your master template...

Yep, for this particular case, the master template might be the best
way to go. It won't hurt anything if you have it on a page without a
form.

But, I think the fantastic thing about Form Widgets is how easy it is
to customize them to get the look and feel that you want.

Alberto Valverde

unread,
Nov 11, 2006, 12:49:48 PM11/11/06
to turbo...@googlegroups.com
"Mark Ramm" <mark.mch...@gmail.com> writes:
>> You could grab the template for the TableForm widget, add this
>> little
>> snippit to the top of it and overide the template param of TableForm,
>> and and get what you want.
>>
>> If you need this kind of form a lot, create your own CustomFormWidget
>> class, and set it up with it's own default template param.
>
> Or put this on your master template...

You could also add a JSSource to the form's javascript:

class MyForm(TableForm):
javascript = [JSSource("""
addLoadEvent(function(){


if (document.forms.length > 0)
document.forms[0].elements[0].focus();
});

""")]

Alberto

Ian Wilson

unread,
Nov 11, 2006, 2:38:52 PM11/11/06
to turbo...@googlegroups.com
That is exciting. Does that work for CSS as well? I mean just
including it raw? I knew you could include outside files for both css
and js but not just raw code. That is so much better.

-Ian

Alberto Valverde

unread,
Nov 11, 2006, 3:25:57 PM11/11/06
to turbo...@googlegroups.com

On Nov 11, 2006, at 8:38 PM, Ian Wilson wrote:

>
> That is exciting. Does that work for CSS as well? I mean just
> including it raw? I knew you could include outside files for both css
> and js but not just raw code. That is so much better.
>
> -Ian

Yep, but you need to use CSSSource for that in order to get the
<style> tags instead of the <script> ones.

Alberto

Kevin Monceaux

unread,
Nov 13, 2006, 5:28:24 PM11/13/06
to Turbo...@googlegroups.com
Karl,

On Fri, Nov 10, 2006 at 02:19:11PM -0500, Karl Guertin wrote:

> *most* 0.9 stuff will work with 1.0b, the tutorial looks valid from my
> glance at it.

Well, since the tutorial is a bit sketchy on the details I downloaded the
source code for the tutorial and and tried running it with 1.0b1. When I
tried to start it via start-formtutorial.py I got:

Traceback (most recent call last):
File "./start-formstutorial.py", line 19, in ?
modulename="formstutorial.config")
File "/usr/lib/python2.4/site-packages/TurboGears-1.0b1-py2.4.egg/turbogears/config.py", line 207, in update_config
configure_loggers(configdict)
File "/usr/lib/python2.4/site-packages/TurboGears-1.0b1-py2.4.egg/turbogears/config.py", line 136, in configure_loggers
config["global"]["tg.new_style_logging"] = False
KeyError: 'global'


P.S. The tutorial in question is:

http://trac.turbogears.org/turbogears/wiki/SimpleWidgetForm

Mark Ramm

unread,
Nov 13, 2006, 5:42:07 PM11/13/06
to turbo...@googlegroups.com

Try quickstarting a new project with the same name, and copying in the
controllers.py model.py, and templates folder from the tutorial's
tarball.

There are a few configuration changes since that particular tutorial
was written, but I think the actual widget code should still work.

--
Mark Ramm-Christensen
email: mark at compoundthinking dot com
blog: www.compoundthinking.com/blog

Karl Guertin

unread,
Nov 13, 2006, 5:44:57 PM11/13/06
to turbo...@googlegroups.com
On 11/13/06, Kevin Monceaux <Ke...@rawfeddogs.net> wrote:
> Well, since the tutorial is a bit sketchy on the details I downloaded the
> source code for the tutorial and and tried running it with 1.0b1. When I
> tried to start it via start-formtutorial.py I got:

The config format changed several times over the course of 0.9
development. It seems that the tutorial code was written before the
final change to the current format. The easiest solution is usually to
quickstart an appropriately named new project and copy the code
(controllers.py, model.py, templates/*, etc) over to the new project.

I have enough planned for this week that I do not plan to do any
widgets documentation conversion until at least the weekend.

Kevin Monceaux

unread,
Nov 14, 2006, 8:41:01 AM11/14/06
to Turbo...@googlegroups.com
Mark,

On Mon, Nov 13, 2006 at 05:42:07PM -0500, Mark Ramm wrote:

> Try quickstarting a new project with the same name, and copying in the
> controllers.py model.py, and templates folder from the tutorial's
> tarball.

Thanks. That got me going. I did get a couple of DeprecationWarning
messages saying that the use of WidgetsDeclaration is deprecated and
suggesting one use WidgetsList instead. I'll probably have time a littler
later today to play around more with widgets. Can I just swap out
WidgetsList for WidgetsDeclaration?

Kevin Monceaux

unread,
Nov 14, 2006, 9:56:16 AM11/14/06
to Turbo...@googlegroups.com
On Tue, Nov 14, 2006 at 07:41:01AM -0600, Kevin Monceaux wrote:

> Thanks. That got me going. I did get a couple of DeprecationWarning
> messages saying that the use of WidgetsDeclaration is deprecated and
> suggesting one use WidgetsList instead. I'll probably have time a littler
> later today to play around more with widgets. Can I just swap out
> WidgetsList for WidgetsDeclaration?

Looks like I answered my own question. I changed WidgetsDeclaration to
WidgetsList and got rid of that warning. Then, I got a couple of warning
saying that std was depreciated and tg should be used instead in templates.
I changed all occurances of std in the templates to tg and it now runs on
1.0b1 warning free. If it would help I'd be happy to share the updated
source. It can be found at:

http://www.RawFedDogs.net/FormsTutorial-1.0b1.tar.gz

Mark Ramm

unread,
Nov 15, 2006, 1:02:39 PM11/15/06
to turbo...@googlegroups.com
> Looks like I answered my own question. I changed WidgetsDeclaration to
> WidgetsList and got rid of that warning. Then, I got a couple of warning
> saying that std was depreciated and tg should be used instead in templates.
> I changed all occurances of std in the templates to tg and it now runs on
> 1.0b1 warning free.

Good work!

> If it would help I'd be happy to share the updated
> source. It can be found at:
>
> http://www.RawFedDogs.net/FormsTutorial-1.0b1.tar.gz

That's definitely helpful. Thanks!

Ian Wilson

unread,
Dec 7, 2006, 9:52:41 PM12/7/06
to turbo...@googlegroups.com
css = [CSSSource("""
span.boldHeading {
color: #ffffff;
}""")]

Something like this was mentioned earlier in this thread. Besides the
seemingly uselessness of this statement is there any reason why this
would not work inside a widget?

-Ian

Reply all
Reply to author
Forward
0 new messages