我无法验证Django表单中的文件字段

2024-06-14 09:52:44 发布

您现在位置:Python中文网/ 问答频道 /正文

形式

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")

视图:

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)

和模板:

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

  {{form}}

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

每当我提交从计算机中选择的文件时,文件字段将重置为“未选择文件”,错误显示为“此字段是必需的”。其他输入似乎工作正常,如果它们没有错误。我不明白


Tags: 文件textfromformrequesthelpcolumnforms