Django modelform and CSS styling

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'
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容