Handling urls with dynamic parameters in cherrypy / mako

I have a fairly simple web application written in Python using cherrypy and Mako patterns. The most tedious thing is printing links with parameters, especially when I need to gradually add additional parameters, update an existing parameter, or remove a parameter.

A common pattern in an application is to incrementally restrict the items listed in the table. list of images stored in the database. Then the user

  • starts with all the images listed, i.e. the url / images
  • restricts items to categories, so the / images? category = 2398
  • restricts items introduced in 2013/01/07, so the / images? category = 2398 & date = 20130107
  • overrides the category restriction, so the / images? date = 20130107
  • sorts items by size, / images? date = 20130107 & sort = size & order = asc
  • sorts items backwards, / images? date = 20130107 & sort = size & order = desc

Most of the time this happens when you click on a table header or a value in a table cell (like a category name) and handle a lot of code that more or less remains the same and quite tedious to write and change.

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

+3


source to share


1 answer


You can put 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 make your Maco template build a url like ...



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

+1


source







All Articles