This module provides several extra route classes for convenience: domain and subdomain routing, prefixed routes or routes for automatic redirection.
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: |
|
---|
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: |
|
---|
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: |
|
---|
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: |
|
---|
webapp2_extras.routes.
HandlerPrefixRoute
(prefix, routes)[source]¶Same as NamePrefixRoute
, but prefixes the route handler.
__init__
(prefix, routes)¶Initializes a URL route.
Parameters: |
|
---|