0

Form

from django import forms
    
class XLSForm(forms.Form):
    xlsx_file = forms.FileField(help_text="The XLSX file")
    column_1 = forms.ChoiceField(help_text="Column 1 from XLSX")
    column_2 = forms.ChoiceField(help_text="Column 2 from XLSX")
    kaka = forms.CharField(help_text="Column 2 from XLSX")

View:

def IndexView(request):

    if request.method == 'POST':
        form = XLSForm(request.POST, request.FILES)
    else:
        form = XLSForm()

    context = {
        'form': form
    }

    return render(request, 'xlsplot.html', context)

And template:

<form method="post" novalidate>
  {% csrf_token %}

  {{form}}

  <button type="submit" class="btn btn-primary">Submit</button>
</form>

Whenever I submit with file chosen from computer, the file field is reset to "No file chosen" and error is shown "This field is required". Other inputs seem to work fine, if they are filled no errors. I don't get it.

trshmanx
  • 600
  • 7
  • 16

1 Answers1

0

OK. I'm dumb. I forgot to add enctype="multipart/form-data" to form. Case closed!

trshmanx
  • 600
  • 7
  • 16