How can I tell in my django view, which of the 2 forms' fields were filled?
<p>I want to display both <code>form1</code> and <code>form2</code> on a web page. If <code>form2</code> is filled (<code>if success == True</code>), I want my view to not execute    <code>if form1.is_valid:</code></p>

<p>(which gives errors). How can I access the value of <code>success</code> in views.py?</p>

<p>forms.py</p>

<pre><code>class Form1(forms.Form):
    ....
    ....
    def clean(self):
        ....
        ....

class Form2(forms.Form):
    ....
    ....
    def clean(self):
        ....
        success = False
</code></pre>

<p>views.py</p>

<pre><code>def my_view(request, element_pk):
    ....
    ....
    if request.method == POST:
        form2 = Form2(request.POST)
        form1 = Form1(request.POST)

    if form2.is_valid():
        ....
    if form1.is_valid():
        ....

    else:
        form2 = Form2()
        form1 = Form1()
</code></pre>

<p>Setting <code>success = False</code> as you've done in the clean method only sets it locally. Store the value on the class (<code>self.success = False</code>) or in the cleaned_data (<code>self.cleaned_data['success'] = False</code>, assuming your form has no field called success).</p>

<p>Then, you can access the value with:</p>

<pre><code>if form2.is_valid():
    success = form2.success
</code></pre>

<p>or</p>

<pre><code>if form2.is_valid():
    success = form2.cleaned_data.get('success')
</code></pre>

<p>For more information about the <code>clean</code> function, see the <a href="https://docs.djangoproject.com/en/1.10/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other" rel="nofollow">docs</a>.</p>

python django django-forms django-views