Routing

Respite connects views to URLs through resource declarations, each of which define routes for a particular collection of views.

respite.urls.resource(views, routes, prefix='')

Route a collection of views.

Parameters:
  • views – A reference to the class that defines the views.
  • routes – A list of routes.
  • prefix – A string to prefix the routes by, or '' by default.
# urls.py

urlpatterns = resource(
    prefix = 'posts/',
    views = PostViews,
    routes = [
        ...
    ]
)

Routes

There are two ways in which you might populate the resource’s routes: you can declare them inline using the route function, or reference views that have been decorated with the route decorator.

Inline routes

respite.urls.routes.route(regex, view, method, name)

Route the given view.

Parameters:
  • regex – A string describing a regular expression to which the request path will be matched.
  • view – A string describing the name of the view to delegate the request to.
  • method – A string describing the HTTP method that this view accepts.
  • name – A string describing the name of the URL pattern.

regex may also be a lambda that accepts the parent resource’s prefix argument and returns a string describing a regular expression to which the request path will be matched.

name may also be a lambda that accepts the parent resource’s views argument and returns a string describing the name of the URL pattern.

# urls.py

urlpatterns = resource(
    prefix = 'posts/',
    views = PostViews,
    routes = [
        # Route GET requests to 'posts/' to the 'index' view.
        routes.route(
            regex = r'^(?:$|index(?:\.[a-zA-Z]+)?$)',
            view = 'index',
            method = 'GET',
            name = 'blog_posts'
        ),
        # Route GET requests 'posts/1' to the 'show' view.
        routes.route(
            regex = r'^(?P<id>[0-9]+)(?:\.[a-zA-Z]+)?$',
            view = 'show',
            method = 'GET',
            name = 'blog_post'
        )
    ]
)

Referenced routes

respite.decorators.route(regex, method, name)

Route the decorated view.

Parameters:
  • regex – A string describing a regular expression to which the request path will be matched.
  • method – A string describing the HTTP method that this view accepts.
  • name – A string describing the name of the URL pattern.

regex may also be a lambda that accepts the parent resource’s prefix argument and returns a string describing a regular expression to which the request path will be matched.

name may also be a lambda that accepts the parent resource’s views argument and returns a string describing the name of the URL pattern.

# views.py

class PostViews:

    @route(
        regex = r'^(?:$|index(?:\.[a-zA-Z]+)?$)',
        method = 'GET',
        name = 'blog_posts'
    )
    def index(request):
        ...

    @route(
        regex = r'^(?P<id>[0-9]+)(?:\.[a-zA-Z]+)?$',
        method = 'GET',
        name = 'blog_post'
    )
    def show(request, id):
        ...
# urls.py

urlpatterns = resource(
    prefix = 'posts/',
    views = PostViews,
    routes = [
        PostViews.index.route,
        PostViews.show.route
    ]
)

Table Of Contents

Previous topic

Decorators

Next topic

Views

This Page