Router¶
rivr comes with a powerful regex based path router similar to the url patterns system in Django.
Example usage:
import rivr
router = rivr.Router()
@router.register(r'^$')
def index(request):
return rivr.Response('Hello world')
@router.register(r'example/$')
def example(request):
return rivr.Response('Example')
Similar to Django, it will also pull out pattern matches from the regex and feed them as arguments and keyword-arguments to your view. For example:
@router.register(r'^(?P<username>[-\w]+)/$')
def index(request, username):
return rivr.Response('Hello %s' % username)
-
class
rivr.router.
Router
(*urls)¶ -
__init__
(*urls)¶ Router takes URLs which you can register on creation.
Example:
router = rivr.Router( (r'^$', index), (r'^test/$', test), )
-
append_slash
= True¶ When append_slash is True, if the request URL does not match any patterns in the router and it doesn’t end in a slash. The router will HTTP redirect any issues to the same URL with a slash appended.
-
register
(*t)¶ Register a URL pattern with a view. This can either be used as a decorator, or it can be used as a method with a view.
Decorator Example:
@router.register(r'^$') def view(request): return Response()
View Example:
router.register(r'^$', view)
-