I have a relatively simple problem bat as yet I haven found the most appropriate solution.
I have a form which passes several user-selectable values as Get variables. Most of these are BooleanChoice fields and can be selected or not. What I want is a concise way of collecting all the GET keys in a list if the are not empty, and map them to more human readable forms for table headings.
For example if my get variables look like:
?site=1&&startdate=1988-09-09&enddate=1989-09-09&mean_air_temp=True&q=table&submit=Query
I want to make a list like so:
['site', 'startdate', 'endate', 'man_air_temp', ...,]
And then end up with:
['Site Name', 'Start Date', 'End Date', 'Mean Air Temp', ...,]
Which can then be associated with my data as table headings in the template.
I know there are various ways of doing this at length but would like to know if there is a concise, pythonic way of getting the results I want.
Any help much appreciated.
GET parameters are supplied to the view as a dictionary-like object in
request.GET. So, you just need another dictionary to map the keys to your desired headings:The list comprehension simply maps through the list of (key, value) pairs in the GET querydict, and if the value is not empty it looks up the header mapping and adds it to a list.