django csv file validation in model form clean method

Following is the FileModel to upload a csv file in my Django application:

Following is the FileModel to upload a csv file in my Django application:

class File(models.Model):
  uploaded_by = models.ForeignKey(
    User,
    on_delete=models.CASCADE,
  )
  csv_file = models.FileField(
    upload_to='csvfiles/',
  )

On invocation of the /upload_file url pattern, the upload_csv_file view executes as follows:

 def upload_csv_file(request):
   if request.method == 'POST':
      csv_form = CSVForm(request.POST, request.FILES)
      if csv_form.is_valid():
        file_uploaded = csv_form.save(commit=False)
        file_uploaded.uploaded_by = request.user
        csv_form.save()
        return HttpResponse("<h1>Your csv file was uploaded</h1>")

elif request.method == 'GET':
csv_form = CSVForm()
return render(request, './mysite/upload_file.html', {'csv_form': csv_form})

In forms.py I am validating the following:

  • file extension (.csv)
  • file size (5 mb)
class CSVForm(forms.ModelForm):

class Meta:
model = File
fields = ('csv_file',)

def clean_csv_file(self):
uploaded_csv_file = self.cleaned_data['csv_file']

if uploaded_csv_file:
filename = uploaded_csv_file.name
if filename.endswith(settings.FILE_UPLOAD_TYPE):
if uploaded_csv_file.size < int(settings.MAX_UPLOAD_SIZE):
return True
else:
raise forms.ValidationError(
"File size must not exceed 5 MB")
else:
raise forms.ValidationError("Please upload .csv extension files only")

return uploaded_csv_file

def clean(self):
cleaned_data = super(CSVForm, self).clean()
uploaded_csv_file = cleaned_data.get('csv_file')
return uploaded_csv_file

However i encounter the following error on submitting the file upload button:

  Attribute error: 'bool' object has no attribute 'get'

I am unsure whether the 'def clean_csv_file(self)' is being invoked or not.

There are ways to validate file extension and size within the function-based view, but I would like to validate the file attributes in the ModelForm's clean() method itself.

Please suggest a solution to apply the same. Thanks!

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

What’s new in HTML6

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Python Django Tutorial | Django Course

Python Django Tutorial | Django Course

🔥Intellipaat Django course: https://intellipaat.com/python-django-training/ 👉This Python Django tutorial will help you learn what is django web development &...

This Python Django tutorial will help you learn what is django web development & application, what is django and introduction to django framework, how to install django and start programming, how to create a django project and how to build django app. There is a short django project as well to master this python django framework.

Why should you watch this Django tutorial?

You can learn Django much faster than any other programming language and this Django tutorial helps you do just that. Our Django tutorial has been created with extensive inputs from the industry so that you can learn Django and apply it for real world scenarios.

Django

Well i used django ... i want to know after completing all the coding stuff, can i make all the codes online ?

Well i used django ... i want to know after completing all the coding stuff, can i make all the codes online ?

as Django give you a local host, i have a .com domain but can i integrate the codes to it so it works ?