I want to upload a file in 2 steps.
First I want to upload it and show the errors and on the second step I
want actually to save the file data in the database.
So I made a simple form and my problem is how to pass the same file to
the form on the second step?
I mean how to insert request.FILES data in the same form again. Or
maybe there is a better way?
Thanks,
Arshavski Alexander.
First,
Using django's inbuilt File field, you don't store the files in the database, but in the filesystem. You only store the metadata in the database.
Next,
If you want to validate a file input, you can use the standard form validation, where you define the the clean_file_field_name and raise the forms.ValidationError django automatically does the "showing of the page until proper input is provided" for you.
That is the right way of doing it.
Related
As the title suggest.
I have searched Google and stackoverflow, so far I don't find any tutorial that doesn't involve (https://github.com/ImperialCollegeLondon/django-drf-filepond).
While this library seems maintain, at 68 stars, too much risk and I prefer to do without it.
What I tried
When you use filepond input tag with class file-uploader file-uploader-grid, in browser, it will compile and generate a div tag.
The issue is that the id in input will be generated under the div instead of input tag.
Without that id, when the form is submitted, self.request.FILES will be empty dictionary.
So I tried writing a JavaScript to add id to input tag, which don't work unfortunately.
Anyone successfully do it in Django without additional library? Thanks
The input generated is only there to catch files, the actual data is either stored in hidden input fields (if you use server property) or encoded in those fields (if you use file encode plugin).
You can set storeAsFile to true to have FilePond update the fileList property of a file field. But that doesn't work on older versions of iOS, see link in property description:
https://pqina.nl/filepond/docs/api/instance/properties/
I am using Django and All-auth. I want to restrict users to take certain usernames. I know that it is done through writing the following in settings.py.
ACCOUNT_USERNAME_BLACKLIST (=[])
But I want to take this list from a .txt file. Should I read the .txt file in my settings.py and then populate the ACCOUNT_USERNAME_BLACKLIST (=[]) list? Or there is some other way that is more reasonable and considered as a best practice?
Yes. You can just read in the text file in your settings file.
Its a common practice done where people will often load in a json file that contains values for a load of different settings that may change depending on environment.
Is it possible to have a charfield in a form to ask user input a absolute file path then bound the file to Request.file object? I think this is quite routine but I cannot use forms.fileField to do this since I cannot find a argument you can input file path. I searched but seems no related posts can be found.
No, there is no way to do this, because there is no way to give a path to a browser file upload field - for very good security reasons imposed by the browsers themselves.
I need help understanding hot to go about giving my users the ability to upload and remove files in the same form.
Is there a demo some were i can find?
It seems a pretty redundant task but i cant find a documentation on that.
Not talking about admin.
Thanks.
Not sure what you mean by 'in the same form'. Do you want to upload a new file while marking a already uploaded file as to-be-deleted?
You could put a list of existing files as MultipleChoiceField underneath your FileField and delete whichever the user selected as part of your form processing.
There is also a whole lot of 3rd party packages which might suit your needs:
https://www.djangopackages.com/grids/g/file-managers/
Since I can't comment yet.
You probably want to solve it with ajax requests
[___newfile.txt___] [browse] [add] --POST--> /add
- File 1 [Trash] --GET--> /delete/1
- File 2 [Trash]
- File 3 [Trash]
- File 1 [Trash]
I've created a demo project to show how this works.
You can find it at https://github.com/trostik/djangoimagemanage
Hope it helps some one.
Cheers
I am quite confused.
I have wrote an upload form in my site. everything works perfectly..
however, I am reading that I needed to implement a function like handle_uploaded_file to dump the file content from the request.FILES to a physical file.
however , I didn't do it any the file exists in the location I set the upload_to in the FileField field. everything works as expected..
Am I missing something ? why do I need handle_uploaded_file then ?
No, you do not need to do a handle_uploaded_file.
When you save the object (form.save) Django already does this for you.
Look this:
https://github.com/django/django/blob/master/django/db/models/fields/files.py#L270-276