Pyramid: default values ​​in route pattern

I was wondering:
Is it possible to provide default values ​​in the route configuration template?
For example: I have a view that shows a (potentially large) list of files attached to a dataset. I want to split the presentation on pages, each of which shows 100 files. When part of the page in the url template is omitted, I want the first page to display.
So I would like to have something like:

config.add_route('show_files', '/show_files/{datasetid}/{page=1})

Is this, or is an alternative feasible with reasonable effort? I did not find anything in the route syntax description in the pyramid documentation.

Thank you so much!

+3
source share
4 answers

, , - , .

config.add_route('show_files', '/show_files/{datasetid}')
config.add_route('show_files:page', '/show_files/{datasetid}/{page}')

@view_config(route_name='show_files')
@view_config(route_name='show_files:page')
def show_files_view(request):
    page = request.matchdict.get('page', '1')
+12

A (hacky) - . matchdict.

def matchdict_default(**kw):
    def f(info, request):
        for k, v in kw.iteritems():
            info['match'].setdefault(k, v)
        return True
    return f

config.add_route(
    'show_files', 
    '/show_files/{datasetid}/{page}')
config.add_route(
    'show_files', 
    '/show_files/{datasetid}', 
    custom_predicates=(matchdict_default(page=1),))
+1

. , iteritems().

def matchdict_default(**kw):
def f(info, request):
    for k in kw:
        info['match'].setdefault(k, kw[k])
    return True
return f

config.add_route(
'show_files', 
'/show_files/{datasetid}/{page}')
config.add_route(
'show_files', 
'/show_files/{datasetid}', 
custom_predicates=(matchdict_default(page=1),))`

now both of the following urls resolve to the page value, and, urls
can be created without needing to include a query
parameter

/show_files/an_id/
/show_files/an_id/?page=1
0

All Articles