I m iterating through the fields of a form and for certain fields I want a slightly different layout, requiring altered HTML.
To do this accurately, I just need to know the widget type. Its class name or something similar. In standard python, this is easy! field.field.widget.__class__.__name__
Unfortunately, you re not allowed access to underscore variables in templates. Great!
You can test field.field.widget.input_type
but this only works for text/password <input ../>
types. I need more resolution that that.
To me, however difficult it might look, it makes most sense to do this at template level. I ve outsourced the bit of code that handles HTML for fields to a separate template that gets included in the field-loop. This means it is consistent across ModelForm
s and standard Form
s (something that wouldn t be true if I wrote an intermediary Form class).
If you can see a universal approach that doesn t require me to edit 20-odd forms, let me know too!