Captatio
Captatio

Reputation: 51

Django modelform and CSS styling

I'm using modelforms to generate forms.

wiek = models.PositiveIntegerField(verbose_name="Wiek")

Is there a way to assign CSS style to it? I don't want to create html for forms because it is too much work (complex forms). I want to generate them from models, but I want to style it too. How to create seperate CSS for each form?

Thanks for answer in advance

Upvotes: 2

Views: 2900

Answers (1)

dzida
dzida

Reputation: 8981

Well,

option 1: probably you have code like this:

<form action=.......
    {{form.as_something}}
</form>

If so, you can assign some special class to each <form> tag and than style it as you want.

option2: if you need to style each field independently you can use fact that Django by default uses id_name_of_field conevention to name fields ids. So you can use #id_name_of_field in your css to style particular field.

option3: in model form you can assign some attributes to each field's widget like:

self.fields['wiek'].widget.attrs['class'] = 'some-class'

Upvotes: 3

Related Questions