limiting file extension in django form file upload - django

I have forms.Form in django. In this form there is a forms.FileField. This field is rendered in html as
<input id="id_upload-loaded_file" name="upload-loaded_file" type="file">
I need to add accepted files only with ".zip" extension so that form will be rendered as
<input id="id_upload-loaded_file" name="upload-loaded_file" type="file" accept=".zip">
How can I add accepted file extension attribute in django?

This is how I did it:
zip_file = forms.FileField(label='Select zip file', widget=forms.FileInput(attrs={'accept': '.zip'}))

Related

Django as_crispy_field doesn't render the TemporaryUploadedFile data

I have a Django formset that receives the POST request data once submitted, and it saves it (including the uploading file) normally to my database, but if the form is invalid, the page renders again with the same values but missing the selected uploaded file.
I am passing the data to the formset as:
fs = MyFormset(request.POST, request.FILES, .......)
and I am rendering the fields of each form one by one. So, the file field data is:
<div class="col-md-10">
{{ form.included_documents|as_crispy_field }}
</div>
When I submit a new form but it fails in one of the validation criteria, it returns with no file chosen although there was a file chosen. But, if I have a saved form, it renders the uploaded file normally.
Failed Tested Workaround:
According to this Github issue answer, I tested it and it parsed the file input with a better look, but it still fails with parsing the TemporaryUploadedFile.
To make sure that I have the value in my form, I added the below, and it showed the value correctly in the span element not the crispy field element:
<div class="col-md-10">
{{ form.included_documents|as_crispy_field }}
<span>{{ form.included_documents.value }}</span>
</div>
What am I missing here to render the TemporaryUploadedFile in crispy field?

Upload to S3 from request.FILES Django

I am using Django and have a template with the following input field:
<input type="file" name="file_upload">
When the form is submitted, I can access the submitted file like this:
request.FILES['file_upload']
Now that I have this file, I want to upload it to my S3 bucket from the Python backend. How can I do this? Thanks!

Django file upload without model and posted using ajax

How to save a file, uploaded from a form to a local directory without using model? Also the form is being posted using ajax, so how do I render the file information from the html file?
Create an HTML form with
<form method="post" enctype="multipart/form-data" action="your_view_name">
<input name="myfile" type="file" />
</form>
Use AJAX to post the file. If you're using jQuery, see this link on how to post stuff
http://api.jquery.com/jquery.post/
In the view, use request.FILES to access the uploaded file. Use whatever Python APIs you have to save the file to the disk.

How to deselct something when selected previously in FileField Django Form?

I have a form which includes filefield to upload some files.
Sometimes what happens is at first, I select one file from browse to upload but then i realize i do not want to upload it anymore.How can i make it empty again??
Is it like that once this field is filled it cannot be reverted back to empty??
Thanks in advance
This can be accomplished with javascript.
Quoting Clear upload file input field with jQuery post from electrictoolbox.com:
function reset_html(id) {
$('#'+id).html($('#'+id).html()); }
$(document).ready(function() {
var file_input_index = 0;
$('input[type=file]').each(function() {
file_input_index++;
$(this).wrap('<div id="file_input_container_'+
file_input_index+'"></div>');
$(this).after('<input type="button" value="Clear"
onclick="reset_html(\'file_input_container_'+
file_input_index+'\')"
/>');
});
});
If you try to post an un-validated form, when you re-render the form, the filefield will be empty.
See this issue for more explanation..
Django Form File Field disappears on form error
The following works for me. Add a clear button to your html
<input type="submit" name = "submit-clear" value="clear file">
Then in your view handle this button using..
if 'submit-clear' in request.POST:
return HttpResponseRedirect('')
This should clear any filefields but will also remove any other settings the user has made, if you want to preserve other settings and just remove the filefield, then ensure the form does not validate somehow so that it renders again.

Django - HTTP Uploading of Multiple Images

I looked at this question:
Uploading multiple files with Django
but it did not seem to help as I have issues regarding it:
I don't want to deal with flash sessions using SWF Upload and Uploadify because I need to do uploads that only authenticated users can do.
newforms are for older versions of django, I am using 1.3
Using Django, how can I have this HTML form structure:
<form enctype="multipart/form-data" action="." method="post">
<label for="id_image_1">Image 1</label>
<input type="file" name="image[]" id="id_image_1" />
<label for="id_image_2">Image 2</label>
<input type="file" name="image[]" id="id_image_2" />
</form>
and handle it using a view?
If you have a fixed number of filefields, you could simply define a form with enough filefields, or add filefields programatically in a form's constructor. See the Django docs on File Uploads.
If you want some sort of dynamic functionality (a la gmail's "add another file"), then you could define a formset using a form with a single filefield. Display a single form initially and when you want to add another, use a little javascript to produce the new form and update the formset's management form. There are a number of snippets floating around to help you do this, though they may need some tweaking. See the Django docs on File Uploads and Formsets.
Another option may be to use a custom widget and field, though I have not reviewed or tried this.
On the off-chance you aren't aware, the name="image[]" scheme is PHP specific and has no special meaning in other languages, unless you reimplement it.
newforms is what the current forms were called before 1.0. Furthermore, if you got your form validated, http://docs.djangoproject.com/en/dev/topics/http/file-uploads/, you'll have your files as a list (tuple, probably, but sequence anyway) in request.FILES['image'], so just do:
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
for f in request.FILES['image']:
handle_uploaded_file(f)
You'll have to write handle_uploaded_file yourself, the URL explains how