[Fixed]-Django's test client with multiple values for data keys


The simplest way is to specify the values as a list or tuple in the dict:

client.post('/foo', data={"key": ["value1", "value2"]})

Alternatively you can use a MultiValueDict as the value.


Just ran into this issue! Unfortunately, your answer did not work for me, In the FormView I was posting to it would only pull out one of the values included, not all of the values

You should also be able to build a querystring manually and post it using content type x-www-form-urlencoded

some_str = 'key=value1&key=value2&test=test&key=value3'
client.post('/foo/', some_str, content_type='application/x-www-form-urlencoded')


from django.core.urlresolvers import reverse
from django.utils.datastructures import MultiValueDict
from django.utils.http import urlencode

form_data = {'username': 'user name',
             'address':  'street',
             'allowed_hosts': ["host1", "host2"]

response = client.post(reverse('new_user'),
                       urlencode(MultiValueDict(form_data), doseq=True),

Leave a comment