django-formtools icon indicating copy to clipboard operation
django-formtools copied to clipboard

Wizard: Files aren't displayed when going back to a form

Open rixx opened this issue 7 years ago • 4 comments

One of my forms includes a working FileField. But if I upload a file to that step of the wizard, and use the "back" button to return to that step later on, the file is then not shown (but all other data is shown correctly). This is probably related to (or a version of) #23.

rixx avatar Aug 15 '18 08:08 rixx

Hi,

I think this is nearly impossible. File input can have default value due to security reasons.

We work around this issue by making our form validated against previously submitted data.

I.e. cleaned data will be populated not only from request.POST and request.FILES but from session data too.

proofit404 avatar Aug 28 '18 02:08 proofit404

Hi, I am using django-formtools for 5 months now and I still have the same issue as @rixx . Do you have any workaround for this? Thanks!

iamembs avatar Mar 05 '20 05:03 iamembs

Hi I suspect that the "initial" parameter of the form is not populated correctly. In the backend the file seems to be available as expected, also after multiple forward/backward jumps (I verified this with debug-prints). However, when e.g. using the FileInput widget it will not be clear in the frontend that a value has already been provided (due to the way the widget is rendered, as proofit404 said, the file input element cannot have a value).

Now, the actual problem is, that if the FileField is required it will be rendered as input element with the required attribute, and choosing a file will be required even though there is already a value assigend in the backend. The FileField will only render a required attribute if there is no initial value provided and the FileField has not gotten required=False as argument. I verified my assumptions by just changing the required argument from True to False. With this things work as expected except that the file-upload will not be enforced. Sorry for not writing a patch, I tried a few minutes but had no time to dive into the formtools code any further.

Here is the relevant code from django: https://github.com/django/django/blob/master/django/forms/widgets.py#L396

saemideluxe avatar Feb 11 '21 10:02 saemideluxe