[Grok-dev] Re: Uncrufty URLs

Martijn Faassen faassen at startifact.com
Fri May 9 08:34:47 EDT 2008


f f m wrote:
>  I was reading through the tutorials, and I was wondering if there was a way
> to pass parameters to a view method from the URL that isn't ugly, like the
> example "http://localhost:8080/test?value1=3&value2=5"?
> 
> I want to be able to have a URL like "http://example.com/go/Title:Subtitle"
> and have it pass <Title> and <Subtitle> to a method.
> 
> Is there anything like the way routes work in Pylons, or using the regexs in
> Django?

Interesting question. I didn't realize standard URL parameters are now 
considered to be "crufty". That's not a criticism; I'm trying to 
understand the underlying motivation for these non-crufty URLs.

Is there an implied hierarchy here? Could you go to 
http://example.com/go/Title by itself?. If so you can implement this 
using containers, or alternatively, traverse methods. I'd want my URL to 
look like this:

http://example.com/go/<My title>/<My Subtitle>

The implementation using traverse() would be something like this:

class Go(grok.Application, grok.Model):
     def traverse(self, name):
         return self.find_obj_by_title(name)

class TitledObj(grok.Model):
     def traverse(self, name):
         return self.find_obj_by_subtitle(name)

class SubtitledObj(grok.Model):
      pass

(of course these names are rather bad; what *is* the titled object and 
what's the subtitled object? let's model those. Also in many cases you'd 
use a containers instead of custom traversal logic to express this 
hierarchy)

That was assuming that there is some form of nesting going on here. You 
can then hook up views to both the <My title> objects and the <My 
subtitle> objects, something the 'Title:Subtitle' pattern doesn't allow.

Perhaps this is deliberate. If there isn't nesting going on here, I'm 
not sure why you'd want your URLs to look like this, as to me that looks 
like you're pretending there is some form of hierarchy while you don't 
really want to expose it using URLs, which to me sounds quite a bit like 
what URL parameters are for.

You could still implement this, again using 'traverse'

class MyObject(grok.Model):
    def traverse(self, name):
        first, second = name.split(':')
        # do whatever you like here to look up the object

Again, I'm very curious to understand the underlying motivation for this 
desire. The 'Foo:Bar' path step pattern seems to hold the promise of 
hierarchical relationships while possibly not fulfilling it. I imagine 
this desire to address an object by URL without parameters while not 
having a hierarchy occurs more frequently in the case of a relational 
database, where the underlying model may not have such a hierarchy.

While implementing a custom 'traverse' that does a 'split' isn't too 
hard, it may be useful to provide some extension module that helps. If 
this turns out to be generally useful, we should also investigate 
whether there would be ways to fit this cleanly into Grok. Right now I'm 
not altogether sure what this is about exactly, though.

Regards,

Martijn



More information about the Grok-dev mailing list