Django: How do I get an array from a QueryDict in a template? Django: How do I get an array from a QueryDict in a template? arrays arrays

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 %}


You can do it using QueryDict's method lists():

{% for k, v in request.session.lastrequest.lists %}{% if k == 'category' %}{{ v }}{% endif %}{% endfor %}

Although you need to loop through the whole dictionary, it will only enter the if statement once. v is always a list there.