Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyramid: Default values in route pattern

I was wondering:
Is it possible, to provide default values within the pattern of a route configuration?
For example: I have a view that shows a (potentially large) list of files bound to a data set. I want to split up the view in pages, which each page showing 100 files. When the page part in the url pattern is omitted, I want the first page to be shown.
So I'd like to have something like:

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

Is that, or an alternative doable with reasonable effort? I haven't found anything in the route syntax description in the pyramid documentation.

Thanks a lot!

like image 304
Chris Avatar asked Dec 04 '22 04:12

Chris


2 Answers

You're probably content with this answer, but another option is to use multiple routes that dispatch to the same view.

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')
like image 150
Michael Merickel Avatar answered Dec 23 '22 03:12

Michael Merickel


No, but you can use a remainder match to make the page optional, and then decide what page to show in your actual logic.

http://readthedocs.org/docs/pyramid/en/master/narr/urldispatch.html

The other option is to simply have your page be a GET variable rather than part of the URL.

like image 27
Amber Avatar answered Dec 23 '22 03:12

Amber