0

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)
Mark
  • 1,385
  • 3
  • 16
  • 29
  • Possible duplicate of [django submit two different forms with one submit button](https://stackoverflow.com/questions/18489393/django-submit-two-different-forms-with-one-submit-button) – michjnich Aug 21 '19 at 09:08
  • You haven't shown us your template. Also, please use some debugger before asking questions: check in your browser dev tools the HTTP request data that you're submitting (is it what you expect?) and in your python IDE, set a breakpoint in your view and check the `request.POST` contents. – dirkgroten Aug 21 '19 at 09:10
  • @dirkgroten Python debugger isn't a bad idea. Need to learn how to use first though. – Mark Aug 21 '19 at 09:18
  • @Mark the dirty way to debug is to `print` to console. So add some `print(request.POST)` lines for example... – dirkgroten Aug 21 '19 at 10:15
  • But you do need to show us the template. – Daniel Roseman Aug 21 '19 at 10:24

0 Answers0