0

I have a form in my Django project that lets users upload images only if they Png, jpeg of gif but if any other file is uploaded it wont accept the file but neither will it give an error saying wrong file uploaded. Is there any function that can be used to perform this?

  • Seems like the form validation is working since it doesn't allow you to upload wrong file types, but just isn't reporting this back. If your form is rendered with `{{ form }}` you should get all of that for free, but if you're doing something custom in your template you may just need to add something like `{{ field.errors }}` in appropriate places. But we'd need to see some code. https://docs.djangoproject.com/en/1.11/topics/forms/ – Björn Kristinsson Oct 12 '17 at 04:15

1 Answers1

0

For Django 1.11+, See this

Here's the example:

from django.core.validators import FileExtensionValidator 
from django.db import models

 class MyModel(models.Model): 
    pdf_file= models.FileField(upload_to='foo/', validators=[FileExtensionValidator(allowed_extensions=['pdf'])])

You didn't provide any code, so I assumed that you were using a FileField. Hope it can help

scharette
  • 9,437
  • 8
  • 33
  • 67