是否可以在单个标签上以酥脆形式应用不同的类别和属性?

时间:2015-04-20 12:05:07

标签: django django-crispy-forms

我第一次使用脆纸。我想根据每个标签的长度为每个标签使用单独的css_class。并且还想使用css属性作为标签及其各自的字段。我尝试了各种方法但没有成功。请帮帮我。

  

forms.py

''' ~~~ Import Statements ~~~ '''
from django import forms 
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Field ,Fieldset,Div,Row,Reset,Button
from crispy_forms.bootstrap import (PrependedText, PrependedAppendedText,InlineField, FormActions)


class student_personal_infoForm(forms.Form):

first_name = forms.CharField(label="First Name",required=True,max_length=20, widget = forms.TextInput(attrs={'placeholder':'First Name', 'required':True}))
middle_name = forms.CharField(label="",required=False,max_length=20, widget = forms.TextInput(attrs={'placeholder':'Middle Name'}))
last_name= forms.CharField(label="", required=True,max_length=20,widget = forms.TextInput(attrs={'placeholder':'Last Name', 'required':True}))

father_name = forms.CharField(label="Father's Name", required=True,max_length=20,widget = forms.TextInput(attrs={'placeholder':'Father Name', 'required':True}))
mother_name = forms.CharField(label="Mother's Name", required=True,max_length=20,widget = forms.TextInput(attrs={'placeholder':'Mother Name', 'required':True}))

helper = FormHelper()
helper.form_method = 'POST'
helper.label_class = 'col-md-4'
helper.form_class = 'form-inline'
helper.field_class = 'col-md-4 control-label'
helper.layout = Layout(
Div(
    Row(  
        Field('first_name',css_class='control-label'),
        Field('middle_name',css_class='control-label'),
        Field('last_name',css_class='control-label'),
        css_class='row-fluid divs panel')
  ),
Div(
    Row(
        Div(
            Field('father_name', css_class='control-label'), 
            css_class='pull-left'),
        Div(
            Field('mother_name', css_class='control-label'), 
            css_class='col-md-6'),
        css_class='row-fluid divs panel')
    ), 
)

通过使用此代码,我的表单看起来非常难看意味着它没有很好地对齐。请建议我的解决方案。

1 个答案:

答案 0 :(得分:0)

只需将其传递到widget,就像requiredplaceholder

一样
widget=forms.TextInput(attrs={
    'class': 'special',
    'placeholder':'First Name',
    'required':True,
})