Django request QueryDict workaround.

Post date: Jun 24, 2009 9:16:11 AM

A Complete Example

For example, given this HTML form:

<form action="/foo/bar/" method="post">

<input type="text" name="your_name" />

<select multiple="multiple" name="bands">

<option value="beatles">The Beatles</option>

<option value="who">The Who</option>

<option value="zombies">The Zombies</option>

</select>

<input type="submit" />

</form>

if the user enters "John Smith" in the your_name field and selects both “The Beatles” and “The Zombies” in the multiple select box, here’s what Django’s request object would have:

>>> request.GET

{}

>>> request.POST

{'your_name': ['John Smith'], 'bands': ['beatles', 'zombies']}

>>> request.POST['your_name']

'John Smith'

>>> request.POST['bands']

'zombies'

>>> request.POST.getlist('bands')

['beatles', 'zombies']

>>> request.POST.get('your_name', 'Adrian')

'John Smith'

>>> request.POST.get('nonexistent_field', 'Nowhere Man')

'Nowhere Man'