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
> 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:
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
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