Django: How do I get an array from a QueryDict in a template?
You can't. You need to call .getlist('category')
, but you can't call methods with a parameter in a template.
I needed access to a QueryDict too, so I added a filter and it worked for my needs.
Wherever you registered your app filters:
#/templatetags/app_filters.pyfrom django import templateregister = template.Library()# Calls .getlist() on a querydict# Use: querydict | get_list {{ querydict|get_list:"itemToGet" }}@register.filterdef get_list(querydict, itemToGet ): return querydict.getlist(itemToGet)
In template:
{% load app_filters %}{% for each in form.data|get_list:"itemYouAreInterestedIn" %} {{each}}{% endfor %}