building controller-relative urls

1 view
Skip to first unread message

rdmu...@bitdance.com

unread,
Apr 5, 2007, 11:25:39 AM4/5/07
to turbo...@googlegroups.com
I'm sure there's a simple answer to this but I haven't been able to find
it through google or the docs. I want to do a redirect from a controller
to a URL relative to the URL of the controller. For example, my method
signature might be:

def someaction(self, label):

so the URL to get there is

http://myhost/path/to/controller/someaction/somelabel

I want to do

redirect(somemagic)

so that the redirect points at

http://www.myhost.com/path/to/controller

and I want this to work no matter how many arguments I peeled off
the end of the URL in the method signature, and without having
to hardcode /path/to/controller.

I'm guessing this is a common need, so there's probably some simple
way to do it, but I haven't figured it out yet ;)

--RDM

Christopher Arndt

unread,
Apr 5, 2007, 11:53:14 AM4/5/07
to turbo...@googlegroups.com
rdmu...@bitdance.com schrieb:

> I want to do a redirect from a controller
> to a URL relative to the URL of the controller.

> I'm guessing this is a common need, so there's probably some simple


> way to do it, but I haven't figured it out yet ;)

If I understand you correctly, you want to find out, where your controller is
mounted, i.e. what its URL is.

Suprisingly, the solution isn't very obvious, but fortunately this has been
discussed before:

http://tinyurl.com/2p5th6


From the solution presented in this thread, I have created a base class for my
controllers, that provides a 'root_url' property. Here's the code:

class BaseController(controllers.Controller):
"""Base CherryPy controller class with useful utility methods."""

def _get_root_url(self):
return self.breadcrumbs[-1][0]
root_url = property(_get_root_url)

def _get_breadcrumbs(self):
try:
return self._breadcrumbs
except AttributeError:
cherry_trail = cherrypy._cputil.get_object_trail()
href = '/'
crumbs = [('/', 'Home')]
for item in cherry_trail:
if isinstance(item[1], controllers.RootController):
continue
if isinstance(item[1], controllers.Controller):
href = "%s%s/" % (href, item[0])
crumbs.append((href,
getattr(item[1], 'title', item[1].__class__.__name__)))
self._breadcrumbs = crumbs
return crumbs
breadcrumbs = property(_get_breadcrumbs)

HTH, Chris

rdmu...@bitdance.com

unread,
Apr 6, 2007, 5:51:21 PM4/6/07
to turbo...@googlegroups.com
On Thu, 5 Apr 2007 at 17:53, Christopher Arndt wrote:
> rdmu...@bitdance.com schrieb:
>> I want to do a redirect from a controller
>> to a URL relative to the URL of the controller.
>
>> I'm guessing this is a common need, so there's probably some simple
>> way to do it, but I haven't figured it out yet ;)
>
> If I understand you correctly, you want to find out, where your controller is
> mounted, i.e. what its URL is.
>
> Suprisingly, the solution isn't very obvious, but fortunately this has been
> discussed before:
>
> http://tinyurl.com/2p5th6

Almost. I could use what you posted to do what I wanted, by looking
through the object trail for 'self', and then constructing a path
based on the objects in front of it on the list.

I think I'm going to solve this problem a different way, but I'm
going to keep this info tucked away in case of future need. Thanks!

--David

Reply all
Reply to author
Forward
0 new messages