I have a Django Form Field with a MultiWidget that consists of two TextInputs.
When rendering the form in a template there is the handy notation
{{ formname.fieldname }}
for rendering a single field. When I use it for the field with the MultiWidget, it will display both HTML input elements. Is there a slight modification of the notation that will display only the first HTML input element? ({{ formname.fieldname.0 }}
does not work.)
I found a solution to this problem that needs two pieces of code.
First
The
render
method that turns the MultiWidget into a string is relatively long. We need to copy&paste it with a tiny modification in the last line to make it returing an array instead.Second
Writing
{{ formname.fieldname }}
in the template will automatically call the field's unicode method and treat the result as a string. As we want an array instead, we need to circumvent the unicode method and access directly what it will return. And this is the methodas_widget
. Thus, to call the first part ofOurMultiWidget
, we will need this code in the template: