3

I want to check a file type before uploading it by:

content = self.cleaned_data['picture']
content_type = content.content_type.split('/')[0]

When I upload the picture I get an error:

'NoneType' object has no attribute 'content_type'

What can be wrong here?

Hanxue
  • 12,243
  • 18
  • 88
  • 130
hln
  • 1,071
  • 4
  • 21
  • 37

1 Answers1

7

imghdr.what will return image format as string (gif, png etc.) if the image is valid, or None otherwise.

Usage:

import imghdr
imghdr.what(value)

In the above example value can be either a file(-like) object or a filename.

vaultah
  • 44,105
  • 12
  • 114
  • 143