Sunday, 15 May 2011

python - handling URLs with dynamic parameters in cherrypy/mako -



python - handling URLs with dynamic parameters in cherrypy/mako -

i have quite simple web application written in python using cherrypy , mako templates. tedious thing printing links parameters, when need incrementally add together more parameters, update existing parameter or remove parameter.

a mutual pattern in app incremental restriction of items listed in table - e.g. list of images stored in database. user

starts images listed, i.e. url /images restricts items in category url /images?category=2398 restricts items entered on 2013/01/07 url /images?category=2398&date=20130107 drops category restriction url /images?date=20130107 sorts items size, /images?date=20130107&sort=size&order=asc sorts items in opposite direction, /images?date=20130107&sort=size&order=desc

most of happens clicking on table header or value in table cell (e.g. category name) , handling lot of code more or less still same , quite tedious write , modify.

what best way handle automatically? there cherrypy (a tool/plugin) or mako create easier?

you can set urls variables in config file , maintain them in 1 place...

#server.conf [urlvariables] variables: ['date', 'sort', 'order']

then when go render mako template build url so...

urlstring = '' currentvar in cherrypy.request.app.config['urlvariables']['variables']: urlstring += currentvar + '=' + 'yourvalue&' mytemplate = template("<a href='{url}'>click here</a>") homecoming mytemplate.render(url='/images?' + urlstring)

hope helps!

andrew

python cherrypy mako

No comments:

Post a Comment