In Django, how can I keep the value of the File input after it is returned to it through an error?
-
09-10-2019 - |
Question
- Let's say someone submits a form. (I'm using Django's form framework completely)
- The form goes through my models.py and doesn't validate well.
- I redirect them back to the page, and display the form errors.
- All of the text boxes have their initial value in them, so the user doesn't have to retype them. But my "Choose File" lost its value!!! It doesn't have the file picked anymore.
- THe user is now required to select the file again. This sucks!!
Solution
If you validate the file separately you could store it before returning the error and provide an indication to the user that their upload has been saved (optionally allow them to choose between uploading yet another file or use the one from a previous submittal). This is a lot of extra housekeeping which could be streamlined but is unavoidable since, for security reasons, browsers do not allow the server to preselect a value for an <input type="file">
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow