我无法使用formset运行Unit Test.
我尝试做一个测试:
class NewClientTestCase(TestCase):
def setUp(self):
self.c = Client()
def test_0_create_individual_with_same_adress(self):
post_data = {
'ctype': User.CONTACT_INDIVIDUAL,
'username': 'dupond.f',
'email': 'new@gmail.com',
'password': 'pwd',
'password2': 'pwd',
'civility': User.CIVILITY_MISTER,
'first_name': 'François',
'last_name': 'DUPOND',
'phone': '+33 1 34 12 52 30',
'gsm': '+33 6 34 12 52 30',
'fax': '+33 1 34 12 52 30',
'form-0-address1': '33 avenue Gambetta',
'form-0-address2': 'apt 50',
'form-0-zip_code': '75020',
'form-0-city': 'Paris',
'form-0-country': 'FRA',
'same_for_billing': True,
}
response = self.c.post(reverse('client:full_account'), post_data, follow=True)
self.assertRedirects(response, '%s?created=1' % reverse('client:dashboard'))
Run Code Online (Sandbox Code Playgroud)
我有这个错误:
ValidationError:[u'ManagementForm数据丢失或被篡改']
我的看法 …
我在服务器端创建了一个 html 表单。
<form action="." method="POST">
<input type="text" name="foo" value="bar">
<textarea name="area">long text</textarea>
<select name="your-choice">
<option value="a" selected>A</option>
<option value="b">B</option>
</select>
</form>
Run Code Online (Sandbox Code Playgroud)
期望的结果:
{
"foo": "bar",
"area": "long text",
"your-choice": "a",
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找的方法 ( parse_form()) 可以这样使用:
response = client.get('/foo/')
# response contains <form> ...</form>
data = parse_form(response.content)
data['my-input']='bar'
response = client.post('/foo/', data)
Run Code Online (Sandbox Code Playgroud)
如何parse_form()在Python中实现?
这与 Django 无关,尽管如此,Django 中有一个功能请求,但几年前被拒绝: https: //code.djangoproject.com/ticket/11797
我围绕基本lxml答案编写了一个小型Python库:html_form_to_dict