Respite connects views to URLs through resource declarations, each of which define routes for a particular collection of views.
Route a collection of views.
Parameters: |
|
---|
# urls.py
urlpatterns = resource(
prefix = 'posts/',
views = PostViews,
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.
Route the given view.
Parameters: |
|
---|
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'
)
]
)
Route the decorated view.
Parameters: |
|
---|
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
]
)