I'm trying to validate two forms on one page with single submit button. I don't really mind using either forms.Form
or forms.ModelForm
knowing the difference. However, when I submit the form for validation all data is being discarded and I get This field is required
shouting at me from all fields except the timeout
field as that one is optional and defaults to 10.
Please see my files:
this view I'm using when trying to make it work with ModelForm class.
When I'm trying to use just Form
class I uncomment the lines and comment out the .save()
lines:
def index(request):
if request.method == 'POST':
form_tc = TestCaseForm(request.POST)
form_ts = TestCaseSuiteForm(request.POST)
if form_tc.is_valid() and form_ts.is_valid():
# TestCase.objects.create(
# name=form_tc.cleaned_data['name'],
# documentation=form_tc.cleaned_data['documentation'],
# steps=form_tc.cleaned_data['steps'],
# tags=form_tc.cleaned_data['tags'],
# setup=form_tc.cleaned_data['setup'],
# teardown=form_tc.cleaned_data['teardown'],
# template=form_tc.cleaned_data['template'],
# timeout=form_tc.cleaned_data.get('timeout', 10),
# )
#
# TestSuite.objects.create(
# name=form_ts.cleaned_data['name'],
# documentation=form_ts.cleaned_data['documentation'],
# setup=form_ts.cleaned_data['setup'],
# teardown=form_ts.cleaned_data['teardown'],
# force_tags=form_ts.cleaned_data['force_tags'],
# timeout=form_ts.cleaned_data.get('timeout', 10),
# )
form_tc.save()
form_ts.save()
return redirect('/list')
forms.py:
class TestCaseForm(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter Name'}), label='')
documentation = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter Documentation'}), label='')
steps = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter Steps'}), label='')
tags = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter Tags'}), label='')
setup = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter Setup'}), label='')
teardown = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter Teardown'}), label='')
template = forms.CharField(widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter Template'}), label='')
timeout = forms.IntegerField(widget=forms.TextInput(attrs={
'class': 'form-control',
'placeholder': 'Enter Timeout (optional)'}),
required=False, label='')
class Meta:
model = TestCase
fields = [
'name',
'documentation',
'steps',
'tags',
'setup',
'teardown',
'template',
'timeout',
]
models:
name = models.CharField(max_length=200)
documentation = models.CharField(max_length=2048, blank=True)
steps = models.CharField(max_length=2048, blank=True)
tags = models.CharField(max_length=200, blank=True)
setup = models.CharField(max_length=2048, blank=True)
teardown = models.CharField(max_length=2048, blank=True)
template = models.CharField(max_length=200, blank=True)
timeout = models.IntegerField(default=10)