Showing posts with label turbogears. Show all posts
Showing posts with label turbogears. Show all posts

Wednesday, September 26, 2007

Toscawidgets Forms: Passing Compound Widgets

For lack of documentation's sake, and because it wasn't initially obvious to me after poking around in the code (in fact, I got led astray, thinking there was some dot notation I would have to use), this is how you pass values into Compound Widgets in Toscawidgets:

Say you have some Widget like this:
form = Form('FormName',
children=[
FieldSet('FieldSetName',
children=[
TextField('TextFieldName')])
])

and displayed in your template like this:

${ form.display(**form_args)}


then you can pass values into it from your controller like so:
def controller(self):
return dict(form=form,
form_args={'value':{
'FieldSetName':{ 'TextFieldName':
'Desired TextField Value'}}})

I guess it's pretty obvious in retrospect, in fact, the compound Form outputs something much like this (minus the outside dictionary), and those values can be passed right back into the Compound Form again. Perhaps, had I been actually using a Model, or gotten more sleep, this all would have been more natural - but hopefully this helps someone else.

Hint: If you're overriding templates, be sure that you're still passing the right values in to fetch the children arguments, i.e.:


${section.display(value_for(section), **args_for(section))}

Monday, September 24, 2007

Gotcha: Toscawidgets and Mochikit

So, after much annoyingness, I've finally realized why I was having so much trouble using the Mochikit extension to Toscawidgets. As the Toscawidgets docs state, you need to include this template if you're using Genshi, which basically adds these lines (plus matching matching body top and bottom lines):

<link for=\"css in tg_css\" replace=\"css.display()\">
<link for=\"js in tg_js_head\" replace=\"js.display()\">

Now, if you're silly, like me, and don't really pay attention, you just include it, without looking too hard at your master template, which includes these lines by default:

<link for=\"css in tg_css\" replace=\"ET(css.display())\">
<link for=\"js in tg_js_head\" replace=\"ET(js.display())\">

As soon as you put the widget in your controller:

from toscawidgets.widgets.mochikit import mochikit
class Root(controllers.RootController):
@expose("genshi:mumapp.templates.sections")
def info(self):
return dict( form = InformationForm,
js = mochikit )

You start to get funny Genshi errors, mine was "AttributeError: 'Stream' object has no attribute 'tag'" which prompted me to try various combinations of adding ET() and HTML() to my templates, when the problem was that Toscawidgets cleans that whole ET/Genshi issue up and breaks when you leave them in.

So, long story short, add the site template and remove the conflicting lines from your master template. (Though, if you are mixing Toscawidgets and Turbogears widgets, you'll have to do something like what's outlined here.)

Saturday, August 04, 2007

Turbogears Banner

I couldn't find a small Turbogears banner / logo / powered by image to my liking, so I just whipped one up. Also, here's a Python logo I found through a google search. Enjoy.


Wednesday, July 25, 2007

Gotcha: Turbogears urls

So, this didn't really waste too much of my time, but watch for trailing slashes in your links. If you have a setup like (Stolen from the SimpleFormsWidgetTutorial):
comment_form = widgets.TableForm(
fields=CommentFields(),
action="save"
)

@expose(template=".templates.form")
def add(self, tg_errors=None):
if tg_errors:
flash("There was a problem with the form!")
return dict(form=comment_form)

@expose()
@validate(form=comment_form)
@error_handler(add)
def save(self, **data):
#...

And, for whatever reason, your user ends up at "host.com/add/" instead of "host.com/add", the form will display fine, but will point to "host.com/add/save" instead of "host.com/save". Then you'll get all kinds of fun errors, like "TypeError: add() got an unexpected keyword argument ''", and you'll scratch your head, wondering if you got an error that the validation somehow threw and the form error handler somehow didn't catch. Or, at least, I did.

Tuesday, July 17, 2007

Turbogears Interpreter Goodness

I'd just like to say that I officially love working with Turbogears. It's just so great to be able to run interactive commands, with the environment that your code is working in already loaded up. (This isn't advertised enough in the Turbogears docs. They should have you do it in the tutorials) I hope it's still this fun when they switch everything over to Pylons, Genshi, and SQLAlchemy in 2.0. Maybe I am just odd, but it brightened up my so far kinda crappy week.

Monday, July 09, 2007

Gotcha: Unicode Error with SQLObject + Turbogears + mysql

I spent a bit of time this morning trying to figure out why the code I wrote at 3am last night wasn't working. I kept getting a crazy Unicode conversion not supported error, and all the solutions that I found Googling were not satisfactory. Eventually, I found that I had used the wrong validator on one of my fields (specifically, a Date field, though I imagine this will happen with others as well). If you run into this, double check your validators before you do anything crazy like change your model to use all UnicodeCols.

Also, on the subject of validators in Turbogears, I recommend using the FormEncode documentation directly, it's much more complete than what you find listed in Turbogears docs.