Keeping file uploads when form validation fails

I’m new to this forum, so I apologise if re-opening this issue is not the right thing to do. Let me know, and I can post my input/question as a new issue instead.

My requirements does not allow me to split upload of images into a separate form and page, and I’m not keen on the other proposed solutions, as they loose the “atomic” nature of a form submit, which I find very appealing.

My form is a complex form with many fields and high likelihood of form-validations errors needed to be fixed by the user, and now I need to add upload of images to the form as well.

I’m curious as to why you want the image-field to be cleared when input validation of any of the form’s other fields fail? That is not the behaviour of normal fields like charField and such, right? They are obviously re-populated in the POST response form, when the form submit fails validation.

I assume that the reason for image-fields (file field in general) not re-populating their content after a validation error, has to do with the way html-forms in general works, and that it is simply not possible using standard html, as it is for normal form-fields. But seen from a user perspective, it is not optimal behaviour, I would argue.

I have come up with a proposed workaround, where I encode the image as base64 on the client, and then use a hidden charField in the form to transfer the encoded iamge. Do any of you see any drawbacks to this approach (except for a slight increase in data transfer (approx 1/3)?

Thanks a lot for you time
Klaus

Hi @klausfrandsen! Yes, it’s usually best to open a new thread for a new question. Good luck!

@philgyford, Thanks for the advice.

Is that also the case, when my comment heavily rely on the context given in the thread, and it propose an alternative way to solve the original question asked?

Welcome @klausfrandsen !

First, if you notice, I have moved this to a new topic because in addition to the base issue this was originally responding to, other ideas and meta-topics have been raised.

If you are commenting on or providing a solution to the existing issue or question, then yes, it belongs in the current topic.

If you are raising your own question or issue needing assistance, then it’s best to open a new topic, regardless of how similar it may be (or appear to be) to the existing topic.

If that results in you needing to make two posts, then separate the post into two, posting each section appropriately.

There are multiple issues with allowing topics to drift from the orginal question.

  • The majority of topics here are people looking for specific answers to a specific issue or problem. They want an answer to be able to move forward and are not necessarily interested in long-running discussions or extensions to that topic.

  • The discussions can easily move away from the original topic, which means the title no longer applies

  • When there are multiple different issues raised by different people, it can become difficult following the threads of conversation to know which question is being addressed by a particular response.

  • Since only one message can be identified as the “solution”, it can be hard to tell when the original poster has had their question satisfied to their satisfaction, compared to the side topics that have developed.

  • People who have responded to, or follow, a topic receive a notification when a new message has been added. If the topic drifts, those users may not be interested in continuing to follow those topic drifts.

  • When a topic has been marked as solved, people looking for questions to answer will generally skip those, and so the new posts are less likely to be visible.

So while there are no established rules, there are a couple of “rules-of-thumb” to apply.

  • If it’s a specific issue that you are facing, open a new topic, regardless of the apparent similarity to an existing topic. (While I don’t have specific references handy, I do know that there are many examples here where a person has symptoms similar to a previous post, but the root cause and solution are completely different.)

  • If the thread is a discussion thread, feel free to add on to it.

    • Ensure that the additional information directly relates to the stated topic and does not veer off into a side-topic.
  • When in doubt, open a new topic. Include a link to the previous topic if you believe the relationship is important enough to establish addition context.

Now, to address the technical points of your post:

You can argue that, but that’s something that would need to be addressed by the IETF. That’s not something that Django can change.

The only drawback I can immediately see are the size restrictions that would apply at various steps along the way.
Also, if you’re talking about large file uploads (10+ MB), do you really want to send that back down to the client on a validation error?

Ken

1 Like

Thanks a lot for your answer, and I will definitely check out those guidelines before posting next time.

To answer your question, yes, I wan’t to download the image-data to the client in case of a validation failure. This is important, as the user has annotated the images using a client-side image editor before submitting the form. So those annotations would be lost otherwise.

We will most likely rescale the image size to hav a max of around 500kb. But to make matters worse, it is a formset that can contain up to 10 images. So yes, I am worried about total transfer size.