[Answered ]-How to see if a Django QueryDict key matches a pattern


Why not loop through the entire list and then do regex on each item’s name? QueryDict.lists() or QueryDict.values() allows you to get all of it and then loop through that.

Like items(), except it includes all values, as a list, for each member of the dictionary.

For example:

>>> q = QueryDict('a=1&a=2&a=3')
>>> q.lists()
[(u'a', [u'1', u'2', u'3'])]

Link: http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.QueryDict


Just iterate over the keys and keep those you want

>>> post = {'item_id_1': 1, 'item_id_2': 2, 'item_id_3': 3, 'noitem': 0}
>>> dict([(k, v) for k, v in post.items() if k[:8] == 'item_id_'])
{'item_id_3': 3, 'item_id_2': 2, 'item_id_1': 1}

Leave a comment