gpt4 book ai didi

Django 表单集单元测试

转载 作者:行者123 更新时间:2023-12-03 11:36:00 26 4
gpt4 key购买 nike

我无法使用 formset 运行单元测试。
我尝试做一个测试:

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'))
我有这个错误:

ValidationError: [u'ManagementForm data is missing or has beentampered with']


我的看法 :
def full_account(request, url_redirect=''):    
from forms import NewUserFullForm, AddressForm, BaseArticleFormSet

fields_required = []
fields_notrequired = []

AddressFormSet = formset_factory(AddressForm, extra=2, formset=BaseArticleFormSet)

if request.method == 'POST':
form = NewUserFullForm(request.POST)
objforms = AddressFormSet(request.POST)

if objforms.is_valid() and form.is_valid():
user = form.save()
address = objforms.forms[0].save()


if url_redirect=='':
url_redirect = '%s?created=1' % reverse('client:dashboard')
logon(request, form.instance)
return HttpResponseRedirect(url_redirect)
else:
form = NewUserFullForm()
objforms = AddressFormSet()

return direct_to_template(request, 'clients/full_account.html', {
'form':form,
'formset': objforms,
'tld_fr':False,
})
和我的表单文件:
class BaseArticleFormSet(BaseFormSet):

def clean(self):

msg_err = _('Ce champ est obligatoire.')
non_errors = True

if 'same_for_billing' in self.data and self.data['same_for_billing'] == 'on':
same_for_billing = True
else:
same_for_billing = False

for i in [0, 1]:

form = self.forms[i]

for field in form.fields:
name_field = 'form-%d-%s' % (i, field )
value_field = self.data[name_field].strip()

if i == 0 and self.forms[0].fields[field].required and value_field =='':
form.errors[field] = msg_err
non_errors = False

elif i == 1 and not same_for_billing and self.forms[1].fields[field].required and value_field =='':
form.errors[field] = msg_err
non_errors = False

return non_errors

class AddressForm(forms.ModelForm):

class Meta:
model = Address

address1 = forms.CharField()
address2 = forms.CharField(required=False)
zip_code = forms.CharField()
city = forms.CharField()
country = forms.ChoiceField(choices=CountryField.COUNTRIES, initial='FRA')

最佳答案

每个 Django 表单集都带有一个需要包含在帖子中的管理表单。 official docs解释得很好。要在单元测试中使用它,您要么需要自己写出来。 (我提供的链接显示了一个示例),或调用 formset.management_form输出数据。

关于Django 表单集单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1630754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com