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