Django: initial values for a bound form
I ran into a small issue with Django today: what do you do when you need initial values in a bound form (such as a search form that is both initialized and then processed via GET action (so that searches can be bookmarked)?
My first thought was to use ‘initial’ in some form, but that turns out not to work according to the docs:
This is why initial values are only displayed for unbound forms. For bound forms, the HTML output will use the bound data.
Stack Overflow had a few answers, but they were either very clunky (if you have a lot of forms and default values), didn’t work or just were not very elegant: http://stackoverflow.com/questions/16026479/django-forms-default-values-for-bound-forms/39284926#39284926
The solution? It’s simple once you think about it, don’t bind the form unless you actually need to: