[Twisted-web] url.URL.fromRequest gives unexpected behaviour

Donovan Preston dp at ulaluma.com
Tue Sep 14 08:03:22 MDT 2004

On Sep 8, 2004, at 7:35 AM, Matt Goodall wrote:

> Hi,
> URL.fromRequest() currently creates a URL for the path segments that
> have been resolved so far. That means that creating a URL in 
> locateChild
> and a rend_* method gives you a different URL. That's unexpected.

Hi, sorry for not responding for so long.

This is actually intentional behavior and is expected -- but so much of 
the url module seems surprising to other people that I think we have to 
stop and seriously revisit it.

> I think we need more explicit ways of creating a URL to allow for
> different usecases:
> URL.fromString(aStr)
>     create a URL from the string. this is already implemented
> URL.fromRequestURI(aRequest)
>     create a URL that matches the request exactly

This would always return a URL object which could reproduce the URL 
used to make the current request. This would be a good thing. But what 
exactly does the URI at the end of the method name indicate?

> URL.fromContext(aContext)
>     create a URL for handled path segments. kind of like the
>     url.here overlay but not so lazy.

Recently, I changed the way keeping track of the current segments works 
by remembering ICurrentSegments and IRemainingSegments in the context. 
Since each Page gets it's own Context object, URL.fromContext could be 
explicitly given a Context object which is at the level for which you 
want the url generated. For example, if you are requesting the url 
/foo/bar/baz but you want a URL object for just /foo, you could find 
the Context object representing this and do URL.fromContext with it. 
(You could find it in the appropriate locateChild, or you could find it 
by climbing the Context .parent chain). This would be a nice way to 
build breadcrumb trails.

> A better name for fromContext might be fromLocation, fromSegment, etc.
> We can deprecate URL.fromRequest but, until it's removed, make it call
> fromContext.

I don't think we should deprecate fromRequest, just change what it does 
semantically. I don't think anyone was relying on the old behavior, 


More information about the Twisted-web mailing list