3

I do have a quite simple web application written in Python using cherrypy and Mako templates. The most tedious thing is printing links with parameters, especially when I need to incrementally add more parameters, update an existing parameter or remove a parameter.

A common pattern in the app is incremental restriction of items listed in a table - e.g. a list of images stored in a database. Then the user

  1. starts with all images listed, i.e. the URL is /images
  2. restricts to items in a category so the URL is /images?category=2398
  3. restricts to items entered on 2013/01/07 so the URL is /images?category=2398&date=20130107
  4. drops the category restriction so the URL is /images?date=20130107
  5. sorts the items by size, /images?date=20130107&sort=size&order=asc
  6. sorts the items in the opposite direction, /images?date=20130107&sort=size&order=desc

Most of this happens by clicking on a table header or a value in table cell (e.g. category name) and handling it is a lot of code which is more or less still the same and quite tedious to write and modify.

What is the best way to handle this automatically? Is there something for cherrypy (a tool/plugin) or Mako to make this easier?

1 Answers1

1

You can put your urls variables in a config file and keep them all in one place...

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

then when you go to render your Mako template build the url like so...

urlstring = ''
for CurrentVar in cherrypy.request.app.config['urlvariables']['variables']:
    urlstring += CurrentVar + '=' + 'yourvalue&' 

mytemplate = Template("<a href='{url}'>click here</a>")
return mytemplate.render(url='/images?' + urlstring)

Hope this helps!

Andrew

Andrew Kloos
  • 4,189
  • 4
  • 28
  • 36