How can I get the relative path to the controller?

3 views
Skip to first unread message

Christoph Zwerschke

unread,
Jun 20, 2007, 4:54:25 AM6/20/07
to turbo...@googlegroups.com
Maybe somebody has a simple solution for the following problem with TG
controllers.

I have two methods in the same controller like that:

@expose()
def page(self, nr=0):
...

@expose()
def sibling(self):
...

Now I want to generate a *relative* link from "page" to "sibling". The
problem is that if page was called like "page?nr=3", then the link would
be just "sibling", but when page was called as "page/3", then link
should be "../sibling".

So what I need is a relative link to the respective controller, which is
'../' times the number of positional arguments.

Is there an easy way to determine that number?

Of course I could

def page(self, *args):
nr = args and args[0] or 0
base_url = '../'*len(args)

but that's ugly and I loose the possibility to pass the nr parameter as
part of the query string.

Another possibility is to always work with absolute paths, but I prefer
using relative paths as much as possible, and this allows me to put the
controller anywhere in the hierarchy.

-- Chris

Jorge Vargas

unread,
Jun 20, 2007, 9:54:36 AM6/20/07
to turbo...@googlegroups.com
take a look at cherrypy.request, in it you have a lot of convenience
methods that will help you work this out like browser_url,
request.path,etc.

> -- Chris
>
> >
>

Christoph Zwerschke

unread,
Jun 20, 2007, 12:23:22 PM6/20/07
to turbo...@googlegroups.com
Jorge Vargas wrote:
> take a look at cherrypy.request, in it you have a lot of convenience
> methods that will help you work this out like browser_url,
> request.path,etc.

I already had a look there but found nothing really convenient.

Looking at request.params does not help distinguishing between 'page'
and 'page/0' in my example.

Even evaluating request.path does not help much if I do not want to
require knowledge of the absolute path of the controller (I want my
method to be independent of that). Think of another example like this:

@expose()
def buffalo(type='buffalo', subtype='buffalo'):
...

Looking at request.path gives me something like
'myapp/buffallo/cow/buffallo/buffallo/buffallo'.
It is impossible to deduce the relative path to the controller from that
which could be '' or '../' or '../../' depending on whether my method
belongs to the cow controller or to a subcontroller with the name
'bufallo'. A constructed example, but I was looking for a simple
solution for the general case.

Am I missing an important method or property?

-- Chris

fumanchu

unread,
Jun 20, 2007, 2:02:28 PM6/20/07
to TurboGears
On Jun 20, 9:23 am, Christoph Zwerschke <c...@online.de> wrote:
> Jorge Vargas wrote:
> > take a look atcherrypy.request, in it you have a lot of convenience

CherryPy 2 doesn't have any single method to do what you want, but
CherryPy 3 does via cherrypy.url(..., relative=True) [1]. That method
will create one URL relative to another URL (the current URL, by
default). Since the mapping from URL's to controllers is not 1:1 (and
sometimes is not even decidable, and certainly not in the opposite
direction), it would be MUCH harder to write a general solution for
creating a new URL relative to a *controller*. Stick with URL's and
stay sane. ;)


Robert Brewer
System Architect
Amor Ministries
fuma...@amor.org

[1] http://www.cherrypy.org/browser/trunk/cherrypy/__init__.py#L487

Reply all
Reply to author
Forward
0 new messages