How to add css_class to django ModelForm using crispy_forms

I have a ModelForm in django and I want to add desired classes to its fields using crispy_forms. this is my form:

class ProductInventoryForm(ModelForm):
    class Meta:
        model = Product
        fields = ["sku", "regular_price", "sale_price", "stock_quantity"]

    def __init__(self, *args, **kwargs):
        super(ProductInventoryForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Field("sku", css_class="your-class"),
            Field("regular_price", css_class="another-class"),
            Field("sale_price", css_class="another-class"),
            Field("stock_quantity", css_class="another-class"),
        )

As you see I used init method and crispy helper and Layout, but it doesn't affect and no classes added to my form.

Back to Top