Django modelform and CSS styling

2018-01-19  本文已影响0人  寂寞杰罗

A

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

Q

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'
上一篇 下一篇

猜你喜欢

热点阅读