I'm trying to figure out why I'm having problems with file upload in Django... I made sure to create the directory and also made sure it has the appropriate permissions. This is working on my local machine; but failing with "Bad Request (400)" when I'm running it on OpenShift. I did get it working once, but it stopped working after that and I can't figure it out; I would greatly appreciate a second set of eyes.
The File module code:
from django.db import modelsfrom django.dispatch import receiverfrom BAGCA.settings import MEDIA_ROOT_FILESclass Files(models.Model): class Meta: verbose_name_plural = "Files" def __str__(self): return u'%s' % self.filename filename = models.CharField(max_length=100) file = models.FileField(upload_to=MEDIA_ROOT_FILES)# These two auto-delete files from filesystem when they are unneeded:@receiver(models.signals.post_delete, sender=Files)def auto_delete_file_on_delete(sender, instance, **kwargs): """Deletes file from filesystem when corresponding `Files` object is deleted. """ if instance.file: if os.path.isfile(instance.file.path): os.remove(instance.file.path)@receiver(models.signals.pre_save, sender=Files)def auto_delete_file_on_change(sender, instance, **kwargs): """Deletes file from filesystem when corresponding `Files` object is changed. """ if not instance.pk: return False try: old_file = Files.objects.get(pk=instance.pk).file if not old_file: return False except Files.DoesNotExist: return False new_file = instance.file if not old_file == new_file: if os.path.isfile(old_file.path): os.remove(old_file.path)
I've been working at this for a while, and I can't figure out why this isn't working... I would greatly appreciate any information in resolving this error.
PythonWeb FrameworksWeb DevelopmentWeb Languages and StandardsAJAX
This method:
- Reads in a file from a file input form
- Writes the file to a directory on the system
- Reads the file back in and inserts data into a database
- Deletes the file
Its a little crazy that my method works; but Django's built in functionality fails...
I know better than to place the blame on Django; but I can't figure out what's going on.
Glad you found it.
It is still strange, that you found nowhere trace logfiles etc. to indicate this.
True in my personal projects all my data was below MEDIA_ROOT, that's why I never enncountered this issue
Errang Genevre
ASKER
Yea, there was a python.log file; but all it had was an entry that a 400 response has been sent.
And yea, I didn't want to put it under MEDIA_ROOT because it was under version control and didn't want the files to be overwritten.
gelonida
ah I see.
In my projects mediaroot was always empty.
What you could do (not sure it makes sense for ypour project)
have your version controlled files in one directory and either copy (or symlink) them during server installation.
And my files are a few kb; nothing gigantic