django-file-upload

ImageField / FileField Django form Currently unable to trim the path to filename

对着背影说爱祢 提交于 2019-12-02 04:05:12
I have a ImageField that stores in AWS S3 (similar to FileField). In the form, it has this "Currently" label that shows the image file path. I would like to trim and just show the filename. referring to the latest answer in Django : customizing FileField value while editing a model , I still cant get it to work. It shows "Currently" with the file path name like this: https://imgur.com/a/xkUZi form.py class CustomClearableFileInput(ClearableFileInput): def get_template_substitution_values(self, value): """ Return value-related substitutions. """ logging.debug("CustomClearableFileInput %s",value

Saving image/file through django shell

跟風遠走 提交于 2019-11-30 11:24:40
问题 I am trying to save an image file through django shell. My model.py is: class user(models.Model): name=models.CharField(max_length=20) pic=models.ImageField() Everyhing is fine with admin and forms but I want to save image using shell: something like >>>user1=User(name='abc', pic="what to write here") 回答1: 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

Django FileField: How to return filename only (in template)

一曲冷凌霜 提交于 2019-11-30 10:37:46
问题 I've got a field in my model of type FileField. This gives me an object of type File, which has the following method: File.name : The name of the file including the relative path from MEDIA_ROOT . What I want is something like " .filename " that will only give me the filename and not the path as well, something like: {% for download in downloads %} <div class="download"> <div class="title">{{download.file.filename}}</div> </div> {% endfor %} Which would give something like myfile.jpg 回答1: In

Processing file uploads before object is saved

风流意气都作罢 提交于 2019-11-30 10:02:48
I've got a model like this: class Talk(BaseModel): title = models.CharField(max_length=200) mp3 = models.FileField(upload_to = u'talks/', max_length=200) seconds = models.IntegerField(blank = True, null = True) I want to validate before saving that the uploaded file is an MP3, like this: def is_mp3(path_to_file): from mutagen.mp3 import MP3 audio = MP3(path_to_file) return not audio.info.sketchy Once I'm sure I've got an MP3, I want to save the length of the talk in the seconds attribute, like this: audio = MP3(path_to_file) self.seconds = audio.info.length The problem is, before saving, the

Django FileField: How to return filename only (in template)

醉酒当歌 提交于 2019-11-29 21:14:11
I've got a field in my model of type FileField . This gives me an object of type File , which has the following method: File.name : The name of the file including the relative path from MEDIA_ROOT . What I want is something like " .filename " that will only give me the filename and not the path as well, something like: {% for download in downloads %} <div class="download"> <div class="title">{{download.file.filename}}</div> </div> {% endfor %} Which would give something like myfile.jpg In your model definition: import os class File(models.Model): file = models.FileField() ... def filename(self

Processing file uploads before object is saved

僤鯓⒐⒋嵵緔 提交于 2019-11-29 15:13:33
问题 I've got a model like this: class Talk(BaseModel): title = models.CharField(max_length=200) mp3 = models.FileField(upload_to = u'talks/', max_length=200) seconds = models.IntegerField(blank = True, null = True) I want to validate before saving that the uploaded file is an MP3, like this: def is_mp3(path_to_file): from mutagen.mp3 import MP3 audio = MP3(path_to_file) return not audio.info.sketchy Once I'm sure I've got an MP3, I want to save the length of the talk in the seconds attribute,

Django (audio) File Validation

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 06:26:20
I'm experimenting with a site that will allow users to upload audio files. I've read every doc that I can get my hands on but can't find much about validating files . Total newb here (never done any file validation of any kind before) and trying to figure this out. Can someone hold my hand and tell me what I need to know? As always, thank you in advance. You want to validate the file before it gets written to disk. When you upload a file, the form gets validated then the uploaded file gets passed to a handler/method that deals with the actual writing to the disk on your server. So in between

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

两盒软妹~` 提交于 2019-11-28 04:09:54
I have a model with a FileField , which holds user uploaded files. Since I want to save space, I would like to avoid duplicates. What I'd like to achieve: Calculate the uploaded files md5 checksum Store the file with the file name based on its md5sum If a file with that name is already there (the new file's a duplicate ), discard the uploaded file and use the existing file instead 1 and 2 is already working, but how would I forget about an uploaded duplicate and use the existing file instead? Note that I'd like to keep the existing file and not overwrite it (mainly to keep the modified time

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

白昼怎懂夜的黑 提交于 2019-11-27 19:47:28
My goal is to limit a FileField on a Django ModelForm to PDFs and Word Documents. The answers I have googled all deal with creating a separate file handler, but I am not sure how to do so in the context of a ModelForm. Is there a setting in settings.py I may use to limit upload file types? I handle this by using a clean_[your_field] method on a ModelForm. You could set a list of acceptable file extensions in settings.py to check against in your clean method, but there's nothing built-into settings.py to limit upload types. Django-Filebrowser, for example, takes the approach of creating a list

Django (audio) File Validation

倾然丶 夕夏残阳落幕 提交于 2019-11-27 01:26:36
问题 I'm experimenting with a site that will allow users to upload audio files. I've read every doc that I can get my hands on but can't find much about validating files . Total newb here (never done any file validation of any kind before) and trying to figure this out. Can someone hold my hand and tell me what I need to know? As always, thank you in advance. 回答1: You want to validate the file before it gets written to disk. When you upload a file, the form gets validated then the uploaded file