django add placeholder text to form field

前端 未结 4 1031
感情败类
感情败类 2020-12-19 08:52

In Django I have the below code which is creating a username and password form on an HTML Page:

{{ form.username }} &
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 09:39

    I hope you do have a forms.py file in your project. While creating your form, you can use following to set placeholder for your fields:

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

    If you have ModelForm in your project you can implement as:

     class MyForm(forms.ModelForm):
        class Meta:
            model = User
            widgets = {
                'username': forms.TextInput(attrs={'placeholder': 'username'}),
                 ..........
            }
    

提交回复
热议问题