django-file-upload

update forms.FileField on django forms

柔情痞子 提交于 2019-12-07 05:30:19
问题 I have a model with a FileField in it: class DocumentUpload(models.Model): document_name = models.CharField(max_length=100, blank=True) document_path = models.FileField(upload_to='uploads') and a form which uses this model class DocumentUploadForm(forms.ModelForm): class Meta: model = DocumentUpload When I use the form to create a new upload everything works fine. if request.method == 'POST': form = DocumentUploadForm(request.POST, request.FILES) if form.is_valid(): form.save() However when I

How does one use magic to verify file type in a Django form clean method?

可紊 提交于 2019-12-07 04:45:09
问题 I have written an email form class in Django with a FileField. I want to check the uploaded file for its type via checking its mimetype. Subsequently, I want to limit file types to pdfs, word, and open office documents. To this end, I have installed python-magic and would like to check file types as follows per the specs for python-magic: mime = magic.Magic(mime=True) file_mime_type = mime.from_file('address/of/file.txt') However, recently uploaded files lack addresses on my server. I also do

save multiple uploaded files in django

人盡茶涼 提交于 2019-12-05 03:54:19
I want to upload and save multiple files in my application, I have <input type="text" name="name" value="" /> <input type="file" name="file" multiple/> in my template. when I hit upload, seems form = MyForm(request.POST, request.FILES) is only saving one file which is last in the list of the many uloaded files. How can I be able to save all the uploaded files using the form form = MyForm(request.POST, request.FILES) blah blah ? Thanks Edit Myform is a model form from this model. class Docs(models.Model): name = models.CharField(max_length=128) file = models.FileField(max_length=100, upload_to=

When editing an object in django the ImageField is not populated

有些话、适合烂在心里 提交于 2019-12-04 15:13:26
I'm trying to edit an existing object through a form. Every thing is working fine except for the ImageField not being populated with the current value. Here's the model: class Post(models.Model): author = models.ForeignKey(User, editable=False) slug = models.SlugField(max_length = 110, editable=False) title = models.CharField(verbose_name='Blog Title', max_length=40, blank=False) body = models.TextField(verbose_name='Body') thumbnail = models.ImageField(upload_to = 'posts/%Y/%m') Here's the view @login_required def edit_profile(request, form_class=UserProfileForm, success_url=None, template

Restricting access to private file downloads in Django

℡╲_俬逩灬. 提交于 2019-12-03 16:32:44
问题 I have multiple FileFields in my django app, which can belong to different users. I am looking for a good way to restrict access to files for user who aren't the owner of the file. What is the best way to achieve this? Any ideas? 回答1: Unfortuanately @Mikko's solution cannot actually work on a production environment since django is not designed to serve files . In a production environment files need to be served by your HTTP server (e.g apache, nginx etc) and not by your application/django

Reading file data during form's clean method

元气小坏坏 提交于 2019-12-03 03:52:46
问题 So, I'm working on implementing the answer to my previous question. Here's my model: class Talk(models.Model): title = models.CharField(max_length=200) mp3 = models.FileField(upload_to = u'talks/', max_length=200) Here's my form: class TalkForm(forms.ModelForm): def clean(self): super(TalkForm, self).clean() cleaned_data = self.cleaned_data if u'mp3' in self.files: from mutagen.mp3 import MP3 if hasattr(self.files['mp3'], 'temporary_file_path'): audio = MP3(self.files['mp3'].temporary_file

Trying to use django and dropzone/

冷暖自知 提交于 2019-12-03 03:47:09
I'm trying to use dropzone.js with django. I'm following the somewhat dated guide here ( https://amatellanes.wordpress.com/2013/11/05/dropzonejs-django-how-to-build-a-file-upload-form/ ) I strongly suspect My view is at issue. def test(request): print "test view has been called" if request.method == 'POST': print "test request method is POST" form = UploadFileForm(request.POST, request.FILES) print request print request.FILES if form.is_valid(): new_file = AttachedFiles(attachedfile=request.FILES['file']) new_file.save() id = new_file.pk print id print "test form valid" return HttpResponse

Django : customizing FileField value while editing a model

我是研究僧i 提交于 2019-12-03 03:02:32
I've got a model, with FileField . When I edit this model in a view, I want to change the "current" value of FileField which gets displayed in the view form. Let me explain. models.py: class DemoVar_model(models.Model): ... Welcome_sound=models.FileField(upload_to='files/%Y/%m/%d') forms.py: class DemoVar_addform(ModelForm): ... class Meta: model = DemoVar_model views.py: soundform = DemoVar_addform(instance=ivrobj) .... return render_to_response(template,{'soundform':soundform}, ....) Now I want to edit this model in my view. When I look in browser, I see the form being displayed as Welcome

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

北慕城南 提交于 2019-12-02 13:48:23
问题 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,

django suspicious operation on image upload

白昼怎懂夜的黑 提交于 2019-12-02 11:53:28
问题 In my admin I try to upload an image and after clicking save I get this error: SuspiciousOperation at /admin/the_chooser/book/add/ Attempted access to '/media/51VqHa8exoL.jpg' denied. Request Method: POST Request URL: http://localhost/admin/the_chooser/book/add/ Django Version: 1.5.2 Exception Type: SuspiciousOperation Exception Value: Attempted access to '/media/51VqHa8exoL.jpg' denied. Exception Location: /Users/username/.virtualenvs/django_books/lib/python2.7/site-packages/django/core