美文网首页
Django modelform and CSS styling

Django modelform and CSS styling

作者: 寂寞杰罗 | 来源:发表于2018-01-19 15:46 被阅读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'
    

    相关文章

      网友评论

          本文标题:Django modelform and CSS styling

          本文链接:https://www.haomeiwen.com/subject/ypaooxtx.html