0

My template:

<form method="post" enctype="multipart/form-data">
  {% csrf_token %}
  <table>
  {{ form.as_table }}
</table>
</form>

My view:

def create_batch(request):
    form = BatchForm(request.POST or None, request=request)
    if form.is_valid():
        form.save_batch(request=request, create_batch_flag=True)
        return redirect('list_batch_url')
    return render(request, template_name, {'form': form})

My form:

class BatchForm(ModelForm):

    csv_file = FileField(label='CSV File')
    class Meta:
        model = Batch
        fields = ('project', 'name', 'csv_file', 'filename')

def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        super(BatchForm, self).__init__(*args, **kwargs)

        #limit to projects created by the logged in user
        self.fields['project'].queryset = Project.all_created_by(self.request.user)

def clean(self):

        cleaned_data = super().clean()
        csv_file = cleaned_data.get("csv_file", False)

The clean method does not pick up "csv_file" and the UI throws a "This field is required" on the CSV FILE input button. I cant seem to get what im doing wrong here. Can you please help. Thanks

Umair
  • 317
  • 1
  • 3
  • 9
  • 3
    Does this answer your question? [This field is required error in django](https://stackoverflow.com/questions/13873904/this-field-is-required-error-in-django) – robins_ Feb 26 '20 at 07:18
  • No that does not answer my question. I need the value from the field, "csv_file" but it on the clean() method (in forms.py), it returns False. – Umair Feb 27 '20 at 05:16

0 Answers0