I'm thinking about the DRY way to use field labels for placeholder attribute of my <input>
HTML elements. I'm using django-crispy-forms
.
Right now I have:
class FilterForm(Form):
query = CharField(max_length=50, label='', required=False)
def __init__(self, data=None, files=None, **kwargs):
self.helper = FormHelper()
self.helper.layout = Layout(
Field('query', placeholder='Search ...'),
)
super(FilterForm, self).__init__(data, files, **kwargs)
I'd prefer, however, not to have to set label and placeholder separately, as this for will eventually have many more fields and it's quite verbose.
What are your suggestions?
Currently, hiding labels can be achieved by using the bootstrap helper attribute below:
You still need to define the placeholder using the Field layout object:
Try this:
This DRY solution doesn't require modification of the layout. I suggest making it a mixin:
A DRY solution could be achieved with this __init__ method:
There is the widgets field if you want more control
I ended up just hiding the field labels using css. It's a bit hackish, but works. I still used placeholder="your label" to define the placeholders though.