Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got the following QueryDict object in my request.session.

<QueryDict: {u'category': [u'44', u'46'], u'cityID': [u'null'], u'countryCode': [u''], u'mapCenterLng': [u'2.291300800000009'], u'mapZoom': [u'12'], u'mapCenterLat': [u'47.10983460000001'], u'price_range': [u''], u'textbox': [u'']}>

In a template try to get the category array using:

{{request.session.lastrequest.category}}

but this gives me only the last value of the array. How can I get the entire array?

Thanks

Jul

like image 915
jul Avatar asked Feb 02 '10 20:02

jul


2 Answers

You can't. You need to call .getlist('category'), but you can't call methods with a parameter in a template.

like image 81
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 00:09

Ignacio Vazquez-Abrams


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.py
from django import template
register = template.Library()

# Calls .getlist() on a querydict
# Use: querydict | get_list {{ querydict|get_list:"itemToGet" }}
@register.filter
def get_list(querydict, itemToGet ):

    return querydict.getlist(itemToGet)

In template:

{% load app_filters %}

{% for each in form.data|get_list:"itemYouAreInterestedIn" %}
     {{each}}
{% endfor %}
like image 22
O.H. Avatar answered Sep 27 '22 22:09

O.H.