On Tue, Mar 9, 2010 at 12:28 AM, Joel <[email protected]> wrote:
> My one reservation is that I don't really understand the routes
> configuration that you suggested:
>
> # Route
> map.connect("file", "/my_url/{path:.*}", controller="mycontroller",
> action="my_action")
>
> But that's understandable seeing as I haven't yet learned more than
> the basics of the Routes system.
The first argument is the route name, which can be used in generation.
It's optional. The second is the route path. They keyword args are
hardcoded variables attached to the route.
This route will match URLs like "/my_url/a.txt" and
"/my_url/subdir/a". The match dict returned to Pylons is {"path":
"a.txt", "controller": "mycontroller", "action": "my_action"}. Pylons
uses the controller and action variables to determine which class and
method to call. Pylons also examines the method's arguments, and
passes any routing variables that have the same name ('path' in this
case).
> I ended up using the following, which seems to work fine for my
> purposes (I guess because I am storing all my files in the "files"
> directory without any subdirectories):
>
> # Route
> map.connect('/file/retrieve/{path}', controller='file',
> action='retrieve')
This matches "a.txt" but not "subdir/a.txt", because the default regex
does not match a slash. The route also has no name.
> And I reference my files from within my Mako templates via:
>
> ${h.url_for(controller='file', action='retrieve', path=fileName)}
That works but it's the old way of doing it. 'h.url_for' is being
phased out in favor of 'url'. If you're following the Pylons book it
still uses 'h.url_for' I think.
It's also generally better to generate routes by name. E.g., if the
route is named "file":
url("file", path=fileName)
This ensures you get exactly the route with that name, and not some
other route that happens to have compatible variables.
--
Mike Orr <[email protected]>
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.