Hyphenated (dash) url in TG 1.0

37 views
Skip to first unread message

sams...@gmail.com

unread,
Feb 23, 2009, 8:48:29 PM2/23/09
to TurboGears
How do I get a url with a dash in it in TG 1.0?

Thanks...

Sam

unread,
Feb 25, 2009, 8:21:23 PM2/25/09
to TurboGears
No one knows how to do this? :) Or have I just not been very
clear...

So what I want is a url like http://www.example.com/foo-bar

but obviously...

@expose()
def foo-bar(self):

Doesn't play well with Python....

So how do I do it?

Christoph Zwerschke

unread,
Feb 26, 2009, 8:33:54 AM2/26/09
to turbo...@googlegroups.com
Sam schrieb:

> No one knows how to do this? :) Or have I just not been very
> clear...
>
> So what I want is a url like http://www.example.com/foo-bar
>
> but obviously...
>
> @expose()
> def foo-bar(self):
>
> Doesn't play well with Python....
>
> So how do I do it?

I don't think it's possible with CherryPy (TG1). You can use an '_' as a
replacement for a '.', but there is no replacement char for '-', '+' or
'$'. Maybe you can suggest something on the CherryPy mailing list.

As a workaround, you can do this:

@expose()
def default(self, method):
if method == 'foo-bar':
...
else:
raise HTTPNotFound

Or, you can implement it as fooBar and then use an Apache alias or
rewrite to map foo-bar to fooBar.

In TG2, you would be also able to use routes.

-- Christoph

Nick Murdoch

unread,
Feb 26, 2009, 10:12:39 AM2/26/09
to TurboGears
I came across this problem a while ago and ended up with something
like this:

class MyController(controllers.Controller):
def i_want_dashes(self):
...

setattr(MyController, 'i-want-dashes', MyController.i_want_dashes)


Dispatching to other methods using default sounds good too; I guess
the rest is personal preference.

Cheers,

Nick

Christoph Zwerschke

unread,
Feb 26, 2009, 11:38:55 AM2/26/09
to turbo...@googlegroups.com
Nick Murdoch schrieb:

> I came across this problem a while ago and ended up with something
> like this:
>
> class MyController(controllers.Controller):
> def i_want_dashes(self):
> ...
>
> setattr(MyController, 'i-want-dashes', MyController.i_want_dashes)

Oh, that's really nice and simple! I hadn't thought that Python would
allow you to do that, but actually why not.

-- Christoph

Kevin Horn

unread,
Feb 26, 2009, 1:11:40 PM2/26/09
to turbo...@googlegroups.com
Wow, that _is_ cool.  I guess Python doesn't care if an attribute has dashes, as long as you never have to use an identifier to reference it.

very groovy

Kevin Horn

Jorge Vargas

unread,
Feb 27, 2009, 12:09:00 AM2/27/09
to turbo...@googlegroups.com

Got to love python :)

nice hack.

Reply all
Reply to author
Forward
0 new messages