Django uploads: Discard uploaded duplicates, use existing file (md5 based check)

Thanks to alTus answer, I was able to figure out that writing a custom storage class is the key, and it was easier than expected. I just omit calling the superclasses _save method to write the file if it is already there and I just return the name. I overwrite get_available_name, to avoid getting numbers … Read more

How to limit file types on file uploads for ModelForms with FileFields?

Create a validation method like: def validate_file_extension(value): if not value.name.endswith(‘.pdf’): raise ValidationError(u’Error message’) and include it on the FileField validators like this: actual_file = models.FileField(upload_to=’uploaded_files’, validators=[validate_file_extension]) Also, instead of manually setting which extensions your model allows, you should create a list on your setting.py and iterate over it. Edit To filter for multiple files: def … Read more

Saving image/file through django shell

from django.core.files import File user1=User(name=”abc”) user1.pic.save(‘abc.png’, File(open(‘/tmp/pic.png’, ‘r’))) You will end up with the image abc.png copied into the upload_to directory specified in the ImageField. In this case, the user1.pic.save method will also save the user1 instance. The documentation for saving an ImageField can be found here https://docs.djangoproject.com/en/dev/ref/files/file/

How do you convert a PIL `Image` to a Django `File`?

The way to do this without having to write back to the filesystem, and then bring the file back into memory via an open call, is to make use of StringIO and Django InMemoryUploadedFile. Here is a quick sample on how you might do this. This assumes that you already have a thumbnailed image named … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)