How do I add a placeholder on a CharField in Django?

前端 未结 9 1386
执笔经年
执笔经年 2020-11-28 19:53

Take this very simple form for example:

class SearchForm(Form):
    q = forms.CharField(label=\'search\')

This gets rendered in the templat

9条回答
  •  余生分开走
    2020-11-28 20:09

    Look at the widgets documentation. Basically it would look like:

    q = forms.CharField(label='search', 
                        widget=forms.TextInput(attrs={'placeholder': 'Search'}))
    

    More writing, yes, but the separation allows for better abstraction of more complicated cases.

    You can also declare a widgets attribute containing a => mapping directly on the Meta of your ModelForm sub-class.

提交回复
热议问题