2012-07-20 23 views

回答

13

最簡單的方法是爲listtupledict指定值:

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

或者您可以使用一個MultiValueDict作爲值。

+0

什麼Django表單字段應該是使用清潔/驗證'list'或者你通過client.post發送值的'tuple'? – Medorator 2014-08-09 15:21:23

3

剛剛遇到這個問題!不幸的是,你的答案不適用於我,在FormView我張貼到它只會拉出其中的一個值,而不是所有的值

你應該也能夠建立一個查詢字符串並張貼它使用內容類型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') 
0
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), 
         content_type='application/x-www-form-urlencoded')