mbraak / django-file-form Goto Github PK
View Code? Open in Web Editor NEWDjango-file-form helps you to write forms with a pretty ajax upload
License: Other
Django-file-form helps you to write forms with a pretty ajax upload
License: Other
Sad news but:
Fine Uploader is no longer maintained and the project has been effectively shut down.
For more info, see FineUploader/fine-uploader#2073
Should we find another lib?
Btw, thanks a lot for the django-file-form lib ;)
Hello, thank you for this lib) But I have a problem - I can't use rendering fields manually. For exemple: if put {{ form.input_file }}
in my template - it don`t work. Field rendering without styles and js.
In the same time, tag {{ form.as_p }}
work normal.
If enter the first line of {{ form.as_p }}
, and then {{ form.input_file }}
- field input_file
rendering normal too.
Sorry for my English)
pip install django-file-form
Downloading/unpacking django-file-form
Downloading django-file-form-0.1.4.5.tar.gz
Running setup.py (path:/Users/me/myCode/python/cida/venv_cato/build/django-file-form/setup.py) egg_info for package django-file-form
Downloading/unpacking ajaxuploader==0.3.0.3 (from django-file-form)
Could not find a version that satisfies the requirement ajaxuploader==0.3.0.3 (from django-file-form) (from versions: 0.2.1, 0.2.2, 0.2.3, 0.2.4, 0.2, 0.3.1, 0.3.2, 0.3.3, 0.3.4, 0.3.5, 0.3.6, 0.3.7, 0.3.8, 0.3)
Cleaning up...
No distributions matching the version for ajaxuploader==0.3.0.3 (from django-file-form)
Storing debug log for failure in /Users/me/.pip/pip.log
It would be great if no inline JavaScript would be required. This for better practice and security.
My suggestion:
Use self execute on $(document).ready()
and instead of requiring an argument let users add a data-attribute to the form.
So instead of the inline script initUploadFields($('#example-form'));
use something like <form method="POST" novalidate {{{data-upload-fields}}}>
Then loop over the forms that have this attribute.
Hello! Thanks for the great code. When running the testproject, everything works fine for me. Insertion to database, file upload and successful form submission. Just it gives me the fake "Upload failed" error when I choose any file and upload it. Using chrome inspect element, this is the error log:
POST http://127.0.0.1:8000/upload/handle_upload 500 (INTERNAL SERVER ERROR) fileuploader.js:4048
handleStandardFileUpload fileuploader.js:4048
api.upload fileuploader.js:4134
upload fileuploader.js:3174
qq.FineUploaderBasic._upload fileuploader.js:1547
qq.FineUploaderBasic._uploadFileOrBlobDataList fileuploader.js:1517
qq.FineUploaderBasic.addFiles fileuploader.js:1153
qq.FineUploaderBasic._onInputChange fileuploader.js:1442
qq.UploadButton.onChange fileuploader.js:1218
(anonymous function) fileuploader.js:779
[FineUploader] Error when attempting to parse xhr response text (SyntaxError: Unexpected token T) fileuploader.js:155
qq.log fileuploader.js:155
qq.FineUploaderBasic.log fileuploader.js:1039
qq.UploadHandler.log fileuploader.js:1247
parseResponse fileuploader.js:3828
onComplete fileuploader.js:3877
(anonymous function)
Thanks! :)
It is linked here but I dont actually see the file anywhere: https://github.com/mbraak/django-file-form/blob/master/testproject/django_file_form_example/templates/base.html#L7
I got
for f in self.cleaned_data['attachments'] or []:
> if f.is_placeholder:
E AttributeError: 'InMemoryUploadedFile' object has no attribute 'is_placeholder'
So some returned data is not of type UploadedFileWithId
, rather its parent class InMemoryUploadedFile
. I am checking if I did something wrong, or if django-file-form
needs patched.
Hello,
Would it be possible to have a new package release?
We had a collision of hash because of this issue.
#154
Thank you
For files uploaded via MultiUploadedFileField
the filename extension is not preserved:
In [3]: m.img.url
Out[3]: '/media/materials/2017-03-24-11-57-06_IMG_1780'
which used to be a .JPG
. Same procedure for UploadedFileField
works as expected:
In [11]: w.img.url
Out[11]: '/media/materials/tub1.jpg'
Does the library support multiple uploads? I would like to try implementing with Bootstrap File Input.
https://plugins.krajee.com/file-input
Hi, I'm using your project with Django 1.7 and jQuery 2.1 + Bootstrap 3 + Bootswatch.
The functionality seems to work, files are uploaded (even via DND) and deletable correctly.
However, my forms always display “Processing dropped files...”, even if I didn't yet upload any file. Could this be a problem of hide()/show() BS3 incompatibility? I didn't find the time to investigate yet.
By the way, the "retry" and "delete" links are very "stuck". Do I miss some CSS somewhere ? Didn't find any in your project. Or should your doc mention that we need to customize all styles before using ?
Here is a screenshot:
Sorry for the hush questions, i'm quite in a rush…
regards,
The documentation asks to
{% include 'django_file_form/upload_template.html' %}
but the pip version and the trunk only has upload_widget.htm
. Should upload_widget.html
be included instead?
Hi !
I'd love to see instructions about how to integrate this into the admin ? Maybe the library could provide some facilities for this (a modeladmin mixin) ?
Thanks !
https://github.com/mbraak/django-file-form/blob/master/django_file_form/models.py#L1
I changed this line with import os from path and working now.
Can we add another widget which works just like Django's FileInput
instead of ClearableFileInput
?
Hi,
I'm currently trying to split a form across multiple views using the django-formtools Wizard application. In a number of these subforms I need to upload files and I was hoping to implement django-file-forms' UploadedFileField/MultipleUploadedFileField to do this. In regular, non-wizard, forms these fields always work, however, within wizard related forms I consistently get the following console error: 'upload_url field is empty; aborting initialization'. It isn't clear to me what is causing this error - perhaps the FileFormMixin is not initializing? I was wondering if you had any ideas as to what is causing this/ how it can be resolved?
Thanks in advance for your help and for all your efforts!
Isaac
Not a big deal but instead of
I am wondering if it is better to do
UPLOAD_DIR = os.path.join(settings.MEDIA_ROOT,
getattr(settings, 'FILE_FORM_UPLOAD_DIR', 'temp_uploads'))
because having users to repeat MEDIA_ROOT
in FILE_FORM_UPLOAD_DIR
can be a source of error.
Note that this would also conforms to the convention of other django configurations such as FileField(upload_to)
where upload_to
should be or return a directory under MEDIA_ROOT
.
Is there a way to enable drag and drop? There seems to be support for it in the js, but I can't figure out how to enable it. Or is it a browser support thing?
Are you planning to add support for formsets? I think it would be a great feature.
I tried using using Django's modelformset_factory
with the form and rendering the formset in the template and used your instruction for the single form, but I'm getting this error when uploading a file in one of the forms:
fileuploader.js:203 [Fine Uploader 5.0.8] Error when attempting to parse xhr response text (Unexpected end of input)
Hi,
We have an error, and here the steps to reproduce:
FILE_FORM_MUST_LOGIN = True
in the settings.py file.After doing that, we're unable to upload any file, because the line 52 of uploader.py file checks this constant and if the user is authenticated.
To fix that, we had to modify this line:
if conf.MUST_LOGIN and not request.user.is_authenticated():
into:
if conf.MUST_LOGIN and not request.user.is_authenticated:
Thanks you in advance.
David.
I got the example form to work but when I try to get the form to work with multiple files, it fails to retrieve them all on the backend.
When I print out all the data that it gets, I only get the following
[MultipleFileExampleForm] self.cleaned_data={'title': 'yup', 'input_file': [<InMemoryUploadedFile: PART_1509050059061_image000001.jpg (image/jpeg)>], 'form_id': '9a9740bd-fd2b-4969-a779-4ea0f1795f86', 'upload_url': '/documents/upload/handle_upload', 'delete_url': '/documents/upload/handle_delete'}
You can review my files here. I am sure I am making a dumb mistake somewhere in here but I can't figure out where.
https://gist.github.com/modernNeo/5d2c203c8b98220d78b92ef8ad953faa
There seem to be a migration missing in the python package:
Migrations for 'django_file_form':
/usr/local/lib/python2.7/site-packages/django_file_form/migrations/0002_auto_20170316_0908.py:
- Alter field uploaded_file on uploadedfile
This file is present in the repo.
Django-file-form should support form prefixes.
I think it is useful to display the size of uploaded files in the file list so that users can
File "/virtual/local/lib/python2.7/site-packages/django_file_form/models.py", line 58, in __str__
return str(self.original_filename or '')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0159' in position 17: ordinal not in range(128)
def __str__(self):
return str(self.original_filename or '')
Is neccessary to wrap return value with str
? https://docs.djangoproject.com/en/1.10/ref/models/instances/#other-model-instance-methods
I would maybe recommend six.text_type
instead https://pythonhosted.org/six/#six.text_type
Django-file-form uses an old custom build of fine-uploader. We should update the version; although the library is no longer maintained.
Hi there,
Thanks a lot for the update of django-file-form to remove the deprecated fineuploader.
I would like to request an update to give the option (or just always) pre initialize the
<div class="dff-files">
I have a mutations observer that observes the list of uploads and executed a function every time a file is uploaded, however, my observer does not observe anything since the div is nonexistent before the first file is uploaded.
I hope this change can be made.
Thanks!
The uploader uses the DELETE method for deleting files. Let's use the POST method instead.
The reason is that some configurations do not handle the X_CSRFTOKEN correctly if the request method is DELETE.
Thanks for the previous fixes for my requests, they work great!
Now I found 2 more issues uploading and deleting files.
In the first issue I upload a file in a single upload field in my formtools SessionWizardView. Click next to go to the next page, go back and then try to delete the file. This causes a:
file_form.js:8 Uncaught TypeError: Cannot read property 'url' of undefined
The file still exists on disk, but somehow the object gets 'lost' in a form wizard?
Another (I think related) issue is if there are errors in the file on posting the first page of the form.
What happens then is if you select a new file, the previous file does not get removed, another Delete link shows up and the new file is forever uploading. However, there is no error in the console when this happens.
Please let me know if I can provide more information to debug these issues, for now we will just keep using the 1.0.1 version in production so there is no rush.
Thanks!
Use pathlib2 instead of pathlib. Both are backports of pathlib from python3. Pathlib is not maintained anymore, except for bugfixes.
I am using django-file-form
to upload attachments
to my objects and it is working fine for uploading files. However, when I try to let users update the attachments
, I will need to add the existing attachments (which are supposed to be removed from UploadedFile
already) to the uploaded file list and introduce my own view for users to delete them, while allowing django-file-form
to add new attachments.
What is the recommended way of doing this? I can imagine the following solutions:
UploadedFile
table and allow django-file-form
to add/remove all attachments, and check which attachments are newly added, which are removed, which are replaced etc.remove
link to remove attachments from my model. It would be difficult to implement "replace" action in this method.UploadedFile
as permanent storage for attachments by linking the files to my model using a m2m
relationship. This looks cleanest but I am not sure it can be done within the current framework of django-file-form
because it is designed for temporary storage.I would appreciate any comment as the best way to proceed. Thanks!
I specified
FILE_FORM_FILE_STORAGE = 'django_hashedfilenamestorage.storage.HashedFilenameFileSystemStorage`
with the storage class from https://github.com/ecometrica/django-hashedfilenamestorage/ . The idea is that upon save()
, the SHA1 hash of uploaded_file
will be computed and uploaded_file.file.name
will be changed to the hashed name. The uploaded files could potentially be saved to S3 and duplicated files will not be saved (since their hashes are the same).
It puzzled me for a while that the uploaded_file
returned from self.cleaned_data
does not have SHA1 file.name
. I then searched the source code of django-file-form
and see that UploadedFile
was created
but save()
is not called anywhere.
Is it true that UploadedFile
is never saved so FILE_FORM_FILE_STORAGE
is not used?
Hi,
I use the file-form with Django 1.6.10.
After having upgraded to @master recent version, and done a migrate
, the uploader is broken and the browser console says :
Error: Cannot find template script at ID 'qq-template'!
I tried hunt a little, but didn't find anything relevant at first sight. What could be related to this problem ?
Thanks & regards,
Olivier
In README.md
, this line:
self.delete_temporary_files()
should be
form.delete_temporary_files()
.
P.S: Thanks for this wonderful tool!
Hello,
django-file-form depends on obsolete pathlib module [1] that override the native pathlib module (new in python3.4 [2]) and break other modules that use missing functionalities in this old external pathlib module
That seems dependency can be remove for python3 install without problem.
Big thanks for your work on django-file-form
Simon
[1] https://bitbucket.org/pitrou/pathlib/
[2] https://docs.python.org/3.5/library/pathlib.html
Hi there,
I noticed that eval is used in the javascript file and the CSP set on our website does not appreciate that. Would it be possible to change the uses of eval() (and possibly other functions) in the source code? I really appreciate the work and that this app is actively maintained.
Thanks!
Although django-file-form
supports drag and drop, it is not immediately clear to the users if the widget is droppable. How can I add a rectangle box under the upload widget with something like "Drop your file here"? This is useful also because right now when I drop a file, it could be dropped outside of the drop area and be caught and displayed by the browser.
Observation (might be wrong): The file has to be dropped on the input widget (the button and the text after it), and there is no visual indication that the file is in the right place.
Hi there,
I'm trying to test my own form that uses MultipleUploadedFileField.
I can't figure out how to test for multiple files so I wanted to check out your testproject.
It seems though that your test doesn't upload multiple files without ajax...
I normally test by opening a file on disk and adding that file to the filefield in the dictionary that I post to the page containing the form. However you can't open multiple files and add them all at the same time. Do you have tips to approach this and what does the test for uploading multiple files actually look like?
Thanks!
Regards, Ellen
Having a form with 2 upload fields Django is doing 4 queries to the backend.
2 would be the maximum (one query per field)
1 query total is doable by getting all django_file_form_uploadedfile for a given form_id and then injecting each field_name
+
SELECT ••• FROM "django_file_form_uploadedfile" WHERE ("django_file_form_uploadedfile"."field_name" = '''photo_id''' AND "django_file_form_uploadedfile"."form_id" = '''a1ecf120-38d7-4272-8e37-f95ca8ffea0d''') LIMIT 1
+
SELECT ••• FROM "django_file_form_uploadedfile" WHERE ("django_file_form_uploadedfile"."field_name" = '''photo_id''' AND "django_file_form_uploadedfile"."form_id" = '''a1ecf120-38d7-4272-8e37-f95ca8ffea0d''') ORDER BY "django_file_form_uploadedfile"."created" DESC LIMIT 1
+
SELECT ••• FROM "django_file_form_uploadedfile" WHERE ("django_file_form_uploadedfile"."field_name" = '''photo_id_2''' AND "django_file_form_uploadedfile"."form_id" = '''a1ecf120-38d7-4272-8e37-f95ca8ffea0d''') LIMIT 1
+
SELECT ••• FROM "django_file_form_uploadedfile" WHERE ("django_file_form_uploadedfile"."field_name" = '''photo_id_2''' AND "django_file_form_uploadedfile"."form_id" = '''a1ecf120-38d7-4272-8e37-f95ca8ffea0d''') ORDER BY "django_file_form_uploadedfile"."created" DESC LIMIT 1
Hello,
we want to have multiple fields to upload images.
We have been trying to wrap the UploadedFileField in a Django FormSet without sucess. Do you have any idea how can we achieve this?
Forms.py
class MediaForm_test(FileFormMixin, forms.Form):
image = UploadedFileField( required=False )
name_for = CharField( required=False )
copyright = CharField( required=False )
def __init__(self, *args, **kwargs):
super(MediaForm_test, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_tag = False
MediaFormSet = formset_factory(MediaForm_test, extra=7)
views.py
class NewSubmitView(LoginRequiredMixin, FormView):
use_ajax = True
template_name = 'jurysys/add.html'
def get(self, request,*args, **kwargs):
context = request.GET.dict()
context['pics'] = MediaFormSet
context['pics_helper'] = MediaFormsetHelper()
context['use_ajax'] = True
return render(request, self.template_name, context)
def post(self, request):
pics_f = MediaFormSet(request.POST, request.FILES)
i=0
if pics_f.is_valid():
for pic in pics_f:
pic_ = media()
if pic.is_valid():
pic_.project = project
image = pic.cleaned_data['image']
name = image.name
pic_.filename = name
pic_.save()
pic.delete_temporary_files()
i+=1
else:
print (pics_f.errors)
View tests do not check the frontend part. To fix this, let's convert them to selenium tests.
django-file-form
is based on tus-js-client
. I was checking if there is a solution for users to perform server-to-server upload and came across Uppy. It seems that uppy and tus.io were developed by the same team, and uppy provides server to server uploads from locations such as dropbox and google drive, on top of file upload using tus-js-client. I also came across django-tus which claims to be a "tus server".
I have the following questions
uppy
instead of tus-js-client
to allow server to server upload?tus
server? Does my server needs to be a tus server (using perhaps django-tus
) to allow resumable upload from users? Or, has django-file-form
already made my application a tus server?Is there a way to add limitations, like only allowing images (and not other types of files) to be uploaded and/or only up to a certain size?
Hello. We used to use v0.5
with Fine Uploader's callbacks, like this:
initUploadFields(formElement, {
callbacks: {
onComplete: this.handleOnComplete,
onSubmitDelete: this.handleOnSubmitDelete,
onValidate: this.handleOnValidate
}
});
Are there any plans to reinstate some callbacks? Or even better can you recommend way to implement callbacks on top of v2.0
?
I've noticed that the tus-js-client dependency has some callbacks, but they don't seem to be exposed by the django-file-form
JS API.
Happy to help with implementation. Thank you.
Issue #266: allow relative FILE_FORM_UPLOAD_DIR setting (thanks to Bo Peng)
But in reality File_FORM_UPLOAD_DIR
has to be relative because the following line of code ( FileField's 'upload_to' argument must be a relative path, not an absolute path.)
This is how FILE_FORM_UPLOAD_DIR
should behave but you mentioned that you would like to keep backward compatibility, which allowed absolute path.
Hi,
Do you plan to support https://github.com/skoczen/django-ajax-uploader as a requirement? You have slightly outdated version in the repo which lacks many backends present in the current version of django-ajax-uploader.
Hi,
Trying to use your package according to steps specified in readme. My model looks like this
class Image(models.Model):
fs = FileSystemStorage(location=settings.MEDIA_ROOT)
entity = models.ForeignKey(Entity, on_delete = models.CASCADE)
image = models.FileField(max_length=255, upload_to='uploads', storage = fs)
and my form like this:
class NewEntityForm(FileFormMixin, FilterForm):
....
self.fields['images'] = MultipleUploadedFileField(required = False)
In my views, I'm trying to save form data (it contains various fields, not only images):
class NewEntityForm(LoginRequiredMixin, FormView):
def form_valid(self, form):
img = 'images'
if img in form.cleaned_data and form.cleaned_data[img] is not None:
for each in form.cleaned_data[img]:
Image.objects.create(entity = newentity, image = each)
However, I receive following error:
Internal Server Error: /en/complexapp/new/
Traceback (most recent call last):
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/contrib/auth/mixins.py", line 56, in dispatch
return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/views/generic/edit.py", line 222, in post
return self.form_valid(form)
File "/home/lyssdod/myapp/complexapp/views.py", line 149, in form_valid
Image.objects.create(site = newsite, oftype = i, image = each)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/base.py", line 736, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/base.py", line 820, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/base.py", line 859, in _do_insert
using=using, raw=raw)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/query.py", line 1039, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 1059, in execute_sql
for sql, params in self.as_sql():
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 1019, in as_sql
for obj in self.query.objs
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 1019, in <listcomp>
for obj in self.query.objs
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 1018, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 968, in pre_save_val
return field.pre_save(obj, add=True)
File "/home/lyssdod/myapp/mydjango/lib/python3.4/site-packages/django/db/models/fields/files.py", line 309, in pre_save
if file and not file._committed:
AttributeError: 'bytes' object has no attribute '_committed'
I realize that it's somehow connected with the type I'm using. printing that type gathers <class 'django_file_form.models.UploadedFileWithId'>
, but in your testproject uploading is done without ajax and type passed into form_valid
is basic <class django.core.files.uploadedfile.InMemoryUploadedFile'>
, which does not have problems with insertion into FileField
. How should I change my code to save uploaded objects in the database?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.