In the project's reporting interface I needed lots of filters to be passed around from the reports to the logs viewer. I also needed pagination on queries made by hand (i.e. not using django's models) ... just to give a little background. I'm not really proficient with Django, and there might be a better way to do this, but I wrote 2 tags that helped me dealing with persistence of GET parameters.
The code is posted here: http://www.djangosnippets.org/snippets/1175/
To install, you just drop the file in a "templatetags" directory, add the application to INSTALLED_APPS in settings, and in the template you have to include the templatetags file like so ...
{% load links %}Usage is simple:{% link_persist viewname arg1,arg2,kwarg1=value1,kwarg2=value2 %}This tag has the same behavior as the normal url tag, with a couple of notable differences:- GET parameters are included in the resulting url
- (key,value) arguments become GET arguments in the current url, overriding the value in the current request
- if you specify a value to a key as an empty string, or as a None, that key will be excluded from the resulting url ... useful for not persisting a set of parameters that only make sens for the current url
The other tag:
{% link_add kwarg1=value1,kwarg2=value2... %}This tag adds to the url in the address bar the values specified in the list of named arguments. It has the same behavior as above, so for example, instead of adding to the current url, or replacing an existing value (useful for pagination) you can also remove parameters from the current URL.
~

0 comments:
Post a Comment