django add placeholder text to form field

前端 未结 4 1021
感情败类
感情败类 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:37

    In case if you want to have field name as a placeholder, you can use code below:

    class LoginForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            super(LoginForm, self).__init__(*args, **kwargs)
            for k,v in self.fields.items():
                v.widget.attrs['placeholder'] = k.capitalize()
    

    Otherwise please refere to this answer.

提交回复
热议问题