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.

No comments: