def __init__(self, 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):
"""Initializes a URL route. Extra arguments compared to
:meth:`webapp2.Route.__init__`:
:param 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 :class:`RedirectHandler`. These two are
equivalent::
route = Route('/foo', handler=webapp2.RedirectHandler,
defaults={'_uri': '/bar'})
route = Route('/foo', redirect_to='/bar')
:param 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')
:param 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/``.
"""
super(RedirectRoute, self).__init__(
template, handler=handler, name=name, defaults=defaults,
build_only=build_only, handler_method=handler_method,
methods=methods, schemes=schemes)
if strict_slash and not name:
raise ValueError('Routes with strict_slash must have a name.')
self.strict_slash = strict_slash
self.redirect_to_name = redirect_to_name
if redirect_to is not None:
assert redirect_to_name is None
self.handler = webapp2.RedirectHandler
self.defaults['_uri'] = redirect_to
评论列表
文章目录