django form wizard with file upload multiple not working - django

I am currently digging into django form wizards and I am trying to upload multiple images. Unfortunately, the BaseStorage provided by django form wizard does not handle multiple files. It always assumes a dictionary to be passed. There is a fix posted here.
The problem is now, that the posted code breaks in the render_done step of the form wizard when the form_obj is reevaluated. FileFields do not validate when the file is turned from dict to a list.
Can anybody point me in the correct direction on how to fix this?
How do generic views deal with this issue? Or don't they have this issue at all?

Related

Is there an equivalent of 'list_editable' for FileFields in django?

I'm making a website with django where you can basically upload a file to the django admin and download it whenever you need to view it. Very simple (making it for my dad which wants to organize his excel files for his work).
This is the problem, I hard coded the download path to the file I uploaded, which makes me have to modify the path every time I upload a new file with the same name (since django adds a random string of digits and characters every time you upload a file with the same name as before). What i'm currently doing is really inefficient, and would like to change that as soon as possible.
My idea is to make the FileField name editable in the django admin. I've come across this:
list_editable = ['asd'].
I thought that might work and so I tried it. The results were interesting. I immediately saw a 'Save' button on the bottom of the admin site, which wasn't there before. But I still couldn't edit the FileField. I've searched this on multiple forums, the django documentation and more forums, only to not find anything useful.
This is a picture of how the admin page looks when I added the list_editable to my FileField.
So, I decided to look for an equivalent to see if that works. Again, I searched on multiple forums, but still found nothing.
Any type of answer or recommendation is very much appreciated.
Thanks in advance!!

Django 2.0.3 uploading files by models.FileField()

I am a beginner django developer. I am building my first serious application and I would like to use the mechanism of uploading files to the server. I have searched a large part of web, but I have not found an easy guide anywhere to deal with this issue in django2.
Is there any experienced django ninja that could provide me with a comprehensive process to design this solution, from creating the model through the form and the apparently view? I tried to work with official documentation, but I can not help it.
I will be very grateful! :D
I would like to know what gone wrong with documentation. You should be more specific when asking questions, that helps people who want to answer.
Let me try to answer your question.
You Model will simply contain an extra filefield like this
class MyModel(models.Model):
my_field = models.FileField(upload_to='your_location')
Your form will
Simply a model form which will contain this model name as class Meta
in the view part you will simply handle this as a post request but you have to add request.FILES
So it will be like
If request.method=='POST':
form = MyModelForm(request.POST, request.FILES)
Now let me come to the upload_to argument. You have to pass a string here. The file will be uploaded to that directory within media directory if the media root setup already done by you.

Django form with autocomplete functionality

I am working on a django project with complex forms. In one of my form fields I need following functionality...... Its the text field. As the user starts typing the value the suggestions from existing database should appear in dropdown. Can anyone help me out with this ? Just similar to autocomplete but able to add new values.
This is going to be something in the JQuery/AJAX side of things, not Django. I would read up on the autocomplete functions of JQuery and use AJAX to call your DJango code and receive a populated list, which then displays to the user.
JQuery Autocomplete - Custom Data
If you don't want to deal with JavaScript, you can use a django application called django-autocomplete-light.
You can learn more about it (and get it) here: https://github.com/yourlabs/django-autocomplete-light

re-uploading files when django form fails at some point

Am currently working with django and using the forms to render a form different fields. The problem am facing is that I have file field in the form and sometimes when submitting the form and a different field returns an error, the file field "value" disappear, which means must re-select the file again..
Is there a way to maintain the file even when the form returns an error? Any examples I can look at or anything that can help me?
Thanks
See https://github.com/un1t/django-file-resubmit
In Django project you have forms with FileField, ImageField.
Everything works great, but when ValidationError is raised, you have
to reselect all files and images again. It is kind of annoying.
django-file-resubmit solves this problem. It works with FileField,
ImageField and sorl.thumbnail.ImageField.

How to override/update information from POST when creating model

I have a view that handles a POST request and attempts to create a new object. However, I know that some of the POST'd data is invalid... But I want to fix it and go ahead and create the object.
The only way I can figure out to be able to 'fix' data in a ModelForm is to create a 'is_valid()' form. To do this, I can either create the form with the POST data, or I can create it with an already existing instance. Unfortunately, if I use the POST data, because some of it is invalid, the form won't validate and I am thus unable to get to the data in the form to fix it. If I create it with an already existing instance, this works, but when the form is displayed, any remaining errors are for whatever reason ignored (and thus don't show up on the web page.) I've tried a combination of creating the the Model form from the POST data and giving it an instance, but this doesn't seem to help. Additionally, I've tried modifying (a copy of) the POST data, fixing it, and then creating the ModelForm from the 'fixed' POST data. This sort of works, with the exception that I have some ImageFields in my form, and they seem to just be ignored.
Any help would be greatly appreciated. I have looked at every good page that I can find to no avail.
Perhaps there is a better way to do this? The problem I'm trying to solve is that I want to have a model that contains ImageFields. The first time I put up the form, the user needs to 'upload' images for each of the fields. However, if he doesn't update an image for one of the fields, I want the new form to come up with a Image upload button on the fields where images have not been uploaded, and just a text field with the image name for images that have been uploaded.
Edit 9/15/2010:
Ok, I think I can simplify all of the above question into this:
def testing( request ) :
test_form = UserProfileForm()
valid = test_form.is_valid()
return render( 'testing.tmpl', locals(), request )
When the above code is rendered, the 'valid' shows as False (as one might expect), but the 'test_form' renders without any errors. I've read through (if perhaps not understood?) the documentation on Models and ModelForms, and I see that most of the time a ModelForm (in my case: UserProfileForm) is created with a specified 'instance'. However, 1) I don't have an instance yet, 2) I would still expect the non-instance'd Form to display errors. I'm sure there is something I am missing. Please illuminate. :)
One more thing, which perhaps the answer to the above will answer anyway, but as far as I can tell, the is_valid() call is supposed to call the 'clean()' function I defined for the UserProfileForm. However, (not being a python guru) I placed 'raise ValidationError()' at the top of clean(), and when I run the code, no error is shown. Thoughts?
Update: I figured out the problem and the answer is below. Thanks!
You should have a look at how to clean form fields in django. You could either manipulate the data returned from the form there or make any kind of validation!
If your ImageFields are optional then you can still validate them (that they are otherwise correct).
Then it's a matter of adjusting your template to show either the uploaded file name or an file upload field depending on whether they've already uploaded one or not. Actually, it would probably be better to give them both fields in the first case. That's what the automatic admin does (the upload field is labeled "Change").
Well, after figuring out how to use the python debugger (pdb) and the fact that within emacs it kind of 'just works' (wow!?) I was able to find that my (empty) form was not bound. Googling bound forms pointed me to this page:
http://docs.djangoproject.com/en/dev/ref/forms/api/
RTFM'ing I find that I can pass an empty dictionary to my form and then everything starts to behave as I would expect. So, to summarize, there is a big difference between:
test_form = UserProfileForm()
and
test_form = UserProfileForm( {} )
The second version causes the rendering of the form to show all the errors (and to call 'clean()').
With risk of having this deleted by the moderator ;) Thank you to all those who commented and for your patience with a new django developer.