Extra routes

This module provides several extra route classes for convenience: domain and subdomain routing, prefixed routes or routes for automatic redirection.

class webapp2_extras.routes.DomainRoute(template, routes)[source]

A route used to restrict route matches to a given domain or subdomain.

For example, to restrict routes to a subdomain of the appspot domain:

app = WSGIApplication([
    DomainRoute('<subdomain>.app-id.appspot.com', [
        Route('/foo', 'FooHandler', 'subdomain-thing'),
    ]),
    Route('/bar', 'BarHandler', 'normal-thing'),
])

The template follows the same syntax used by webapp2.Route and must define named groups if any value must be added to the match results. In the example above, an extra subdomain keyword is passed to the handler, but if the regex didn’t define any named groups, nothing would be added.

__init__(template, routes)[source]

Initializes a URL route.

Parameters:
class webapp2_extras.routes.RedirectRoute(template, handler=None, name=None, defaults=None, build_only=False, handler_method=None, methods=None, schemes=None, redirect_to=None, redirect_to_name=None, strict_slash=False)[source]

A convenience route class for easy redirects.

It adds redirect_to, redirect_to_name and strict_slash options to webapp2.Route.

__init__(template, handler=None, name=None, defaults=None, build_only=False, handler_method=None, methods=None, schemes=None, redirect_to=None, redirect_to_name=None, strict_slash=False)[source]

Initializes a URL route. Extra arguments compared to webapp2.Route.__init__():

Parameters:
  • redirect_to

    A URL string or a callable that returns a URL. If set, this route is used to redirect to it. The callable is called passing (handler, *args, **kwargs) as arguments. This is a convenience to use RedirectHandler. These two are equivalent:

    route = Route('/foo', handler=webapp2.RedirectHandler,
                  defaults={'_uri': '/bar'})
    route = Route('/foo', redirect_to='/bar')
    
  • redirect_to_name

    Same as redirect_to, but the value is the name of a route to redirect to. In the example below, accessing ‘/hello-again’ will redirect to the route named ‘hello’:

    route = Route('/hello', handler=HelloHandler, name='hello')
    route = Route('/hello-again', redirect_to_name='hello')
    
  • strict_slash

    If True, redirects access to the same URL with different trailing slash to the strict path defined in the route. For example, take these routes:

    route = Route('/foo', FooHandler, strict_slash=True)
    route = Route('/bar/', BarHandler, strict_slash=True)
    

    Because strict_slash is True, this is what will happen:

    • Access to /foo will execute FooHandler normally.
    • Access to /bar/ will execute BarHandler normally.
    • Access to /foo/ will redirect to /foo.
    • Access to /bar will redirect to /bar/.
class webapp2_extras.routes.PathPrefixRoute(prefix, routes)[source]

Same as NamePrefixRoute, but prefixes the route path.

For example, imagine we have these routes:

app = WSGIApplication([
    Route('/users/<user:\w+>/', UserOverviewHandler,
          'user-overview'),
    Route('/users/<user:\w+>/profile', UserProfileHandler,
          'user-profile'),
    Route('/users/<user:\w+>/projects', UserProjectsHandler,
          'user-projects'),
])

We could refactor them to reuse the common path prefix:

app = WSGIApplication([
    PathPrefixRoute('/users/<user:\w+>', [
        Route('/', UserOverviewHandler, 'user-overview'),
        Route('/profile', UserProfileHandler, 'user-profile'),
        Route('/projects', UserProjectsHandler, 'user-projects'),
    ]),
])

This is not only convenient, but also performs better: the nested routes will only be tested if the path prefix matches.

__init__(prefix, routes)[source]

Initializes a URL route.

Parameters:
  • prefix – The prefix to be prepended. It must start with a slash but not end with a slash.
  • routes – A list of webapp2.Route instances.
class webapp2_extras.routes.NamePrefixRoute(prefix, routes)[source]

The idea of this route is to set a base name for other routes:

app = WSGIApplication([
    NamePrefixRoute('user-', [
        Route('/users/<user:\w+>/', UserOverviewHandler, 'overview'),
        Route('/users/<user:\w+>/profile', UserProfileHandler,
              'profile'),
        Route('/users/<user:\w+>/projects', UserProjectsHandler,
              'projects'),
    ]),
])

The example above is the same as setting the following routes, just more convenient as you can reuse the name prefix:

app = WSGIApplication([
    Route('/users/<user:\w+>/', UserOverviewHandler, 'user-overview'),
    Route('/users/<user:\w+>/profile', UserProfileHandler,
          'user-profile'),
    Route('/users/<user:\w+>/projects', UserProjectsHandler,
          'user-projects'),
])
__init__(prefix, routes)[source]

Initializes a URL route.

Parameters:
  • prefix – The prefix to be prepended.
  • routes – A list of webapp2.Route instances.
class webapp2_extras.routes.HandlerPrefixRoute(prefix, routes)[source]

Same as NamePrefixRoute, but prefixes the route handler.

__init__(prefix, routes)

Initializes a URL route.

Parameters:
  • prefix – The prefix to be prepended.
  • routes – A list of webapp2.Route instances.

Previous topic

Mako

Next topic

Secure cookies

This Page