Dynamic Url patterns

118 views
Skip to first unread message

Fang Zhou

unread,
Aug 22, 2012, 11:01:25 AM8/22/12
to we...@googlegroups.com
Hi all,

I have many controller like

GameController
CardController
PlayerController

and I want to build a dynamic url pattern like

("/%s/*", "%sController")

Does Web.py provide this kind of url pattern? Or I need to write a central controller to dispatch the actions by myself?

Best Regards
Fang Zhou

Anand Chitipothu

unread,
Aug 22, 2012, 12:13:30 PM8/22/12
to we...@googlegroups.com
I think you need to write one your self. It is not that hard.

urls = ("/(.*)/(.*)", "dispatch")

class dispatch:
def GET(self, name, path):
cls = globals().get(name + "Controller")
method = web.ctx.method.upper()
return getattr(cls(), method)(path)
POST = GET

You may have to take care of handling AttributeException and raising
NoMethod etc.

Anand

Fang Zhou

unread,
Aug 22, 2012, 12:16:55 PM8/22/12
to we...@googlegroups.com
Hi Anand

Thanks. I get it.

Best regards,
Fang Zhou(方舟)
> --
> You received this message because you are subscribed to the Google Groups "web.py" group.
> To post to this group, send email to we...@googlegroups.com.
> To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/webpy?hl=en.
>
Reply all
Reply to author
Forward
0 new messages