Form validation failing in Django
<p>Im trying to get a form to validate with a Charfield but using the Select widget.</p>

<p>Here is my <code>view.py</code> code:</p>

<pre><code>def mpld3plot(request):
    form = PlotlyPlotForm()
    form.fields['plot_file'].widget.choices = own_funcs.uploaded_files(string=False)

    if request.method == 'POST':
        print(form.is_valid())
        if form.is_valid():

            return HttpResponseRedirect('/mpld3')

        else:
            pass
    else:
        pass

    return render(request, 'evert/plot.html', {'plottype': 'MPLD3',
                                               'form': form})
</code></pre>

<p>Below is my <code>forms.py</code> code:</p>

<pre><code>class Mpld3PlotForm(forms.Form):

    plot_file = forms.CharField(widget=forms.Select(choices=[('', 'a'), ('', 'b')]))
</code></pre>

<p>The form does not validate on submit. I update the choices dynamically based on uploaded files. Any help would be appreciated. </p>

<p>Turns out I did not pass the <code>request</code> object to the form.</p>

<p>The <code>view.py</code> file should look like the following:</p>

<pre><code>def mpld3plot(request):
    form = PlotlyPlotForm(request.POST)
    form.fields['plot_file'].widget.choices = own_funcs.uploaded_files(string=False)

    if request.method == 'POST':
        print(form.is_valid())
        if form.is_valid():

            return HttpResponseRedirect('/mpld3')

        else:
            pass
    else:
        pass

    return render(request, 'evert/plot.html', {'plottype': 'MPLD3',
                                               'form': form})
</code></pre>

python django forms validation python-3.x