I've got an HTML table that lists a bunch of entries. I want to make the column headers clickable to be sorted. Problem is, I already have a bunch of filters in the URL (stuff like ?min_price=200&max_price=6000
). How can I build the link such that it keeps these params in tact? Is there a function for it? Do I have to do it in the view?
Asked
Active
Viewed 118 times
0

mpen
- 272,448
- 266
- 850
- 1,236
2 Answers
1
Here's a cute trick I found that might do what you want: http://andrewwilkinson.wordpress.com/2009/02/03/using-django-forms-for-get-urls/
Personally, I wouldn't do it that way. I'd use Javascript and change the DOM directly. You're not adding or removing any data if you are just sorting. If you just change the DOM you'll possibly avoid calling the server, and the user experience will be better because you won't need to reload the screen.

jfenwick
- 1,319
- 15
- 17
-
Also...the link you provided.. I'm not sure how URL-encoding a form is even relevant? If what you really mean to say is "copy the GET dict, add in your sort param, and then use urllib to urlencode it" then maybe you have an answer. – mpen Aug 19 '10 at 17:56
-
Have you seen jqgrid? http://www.trirand.com/blog/ It's a fairly sophisticated grid widget. It has dynamic pagination and sorting. – jfenwick Aug 19 '10 at 18:01
-
Not looking for a full-featured spreadsheet...just a simple table. Some cells are more complicated, containing images, or multiple pieces of data. – mpen Aug 19 '10 at 21:32
1
In your template, you can add:
your_current_url?{{ request.META.QUERY_STRING }}
to pass the current query string params on to additional links.
Hope that helps!

Matthew J Morrison
- 4,343
- 3
- 28
- 45
-
Nice. I learned something today :) Also, look out for this when using GET: http://stackoverflow.com/questions/266322/http-uri-get-limit – jfenwick Aug 19 '10 at 17:47
-
I thought it was implicit, but I guess not. Clicking the headers would add something like a `sort_by` param. With your solution, clicking one header, and then another would just keep adding `sort_by` params. It needs to be replaced so there are no dupes. – mpen Aug 19 '10 at 17:52
-
in that case you could write a template tag to pull what you want, or exclude what you don't want from request.META['QUERY_STRING']. – Matthew J Morrison Aug 19 '10 at 18:02
-
1It still doesn't make sense to parse `request.META['QUERY_STRING']` when everything I need is already in `request.GET`, which can easily be `urlencoded` and will automatically prevent dupes given its a dict. – mpen Aug 19 '10 at 21:30
-
right, sorry, that's what I meant. request.GET so you don't have to parse anything, good catch. – Matthew J Morrison Aug 19 '10 at 22:56