Use field label as placeholder in django-crispy-fo

2019-06-18 16:14发布

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?

7条回答
神经病院院长
2楼-- · 2019-06-18 16:42

Currently, hiding labels can be achieved by using the bootstrap helper attribute below:

self.helper.form_show_labels = False

Default set to True. It decides wether to render or not form’s fields labels.

You still need to define the placeholder using the Field layout object:

Field('query', placeholder='Search ...'),

查看更多
相关推荐>>
3楼-- · 2019-06-18 16:46

Try this:

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=kwargs.pop('query_placeholder', 'random text')),
        )
        super(FilterForm, self).__init__(data, files, **kwargs)
查看更多
太酷不给撩
4楼-- · 2019-06-18 16:51

This DRY solution doesn't require modification of the layout. I suggest making it a mixin:

class MyForm(Form):

    _placeholders = {
        'fieldname': 'fieldname placeholder',
    }

    def __init__(self, *args, **kwargs):

        # Assign placeholder to widget of fields
        # listed in self._placeholders.
        for field_name, field in self.fields.items():
            if field_name in self._placeholders:
                self.fields[field_name].widget.attrs['placeholder'] = \
                self._placeholders[field_name]

        super(MyForm, self).__init__(*args, **kwargs)
查看更多
Melony?
5楼-- · 2019-06-18 16:53

A DRY solution could be achieved with this __init__ method:

def __init__(self, *args, **kwargs):
    super(FilterForm, self).__init__(*args, **kwargs)
    helper = self.helper = FormHelper()

    # Moving field labels into placeholders
    layout = helper.layout = Layout()
    for field_name, field in self.fields.items():
        layout.append(Field(field_name, placeholder=field.label))
    helper.form_show_labels = False
查看更多
我命由我不由天
6楼-- · 2019-06-18 16:56

There is the widgets field if you want more control

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        widgets = {
            'comment': forms.Textarea(attrs={'rows': 6, 'placeholder': 'Enter your comments'}),
        }
        labels = {
            "private": "Keep Private",
        }
        exclude = ['response', 'display']
查看更多
放荡不羁爱自由
7楼-- · 2019-06-18 17:02

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.

查看更多
登录 后发表回答