How to add class, id, placeholder attributes to a field in django model forms

前端 未结 9 2089
既然无缘
既然无缘 2020-12-05 00:12

I have a django model like below

models.py

class Product(models.Model):
    name = models.CharField(max_length = 300)
    descripti         


        
9条回答
  •  既然无缘
    2020-12-05 00:44

    You can update forms.py as below

    class ProductForm(ModelForm):
        class Meta:
            model = Product
            exclude = ('updated', 'created')
            widgets={
                       "name":forms.TextInput(attrs={'placeholder':'Name','name':'Name','id':'common_id_for_imputfields','class':'input-class_name'}),
                       "description":forms.TextInput(attrs={'placeholder':'description','name':'description','id':'common_id_for_imputfields','class':'input-class_name'}),
                    }  
    

提交回复
热议问题