Django crispy-forms field_class 没有按预期工作
Django crispy-forms field_class not working as expected
我正在使用 crispy-forms
进行简单构建,但是 field-class
属性没有按预期工作。
表格
class ArrivalForm(forms.Form):
def __init__(self, *args, **kwargs):
super(ArrivalForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'arrival-form'
self.helper.form_class = ''
self.helper.form_method = 'POST'
self.helper.form_action = ''
self.helper.field_class = 'form-control'
self.helper.layout = Layout(
Div(
Div('passenger_name', css_class='col-md-6'),
Div('passenger_lastname', css_class='col-md-6'),
css_class='row'),
)
self.helper.add_input(Submit('submit', 'Submit'))
passenger_name = forms.CharField(
label = "Firstname:",
max_length = 80,
required = True,
)
passenger_lastname = forms.CharField(
label = "Lastname:",
max_length = 80,
required = True,
)
渲染HTML
<form id="arrival-form" method="post" name="arrival-form">
<input name="csrfmiddlewaretoken" type="hidden" value=
"akxd0BVQrwaHbHr4FLjaDLz72BUUN9rQ">
<div class="row">
<div class="col-md-6">
<div class="control-group" id="div_id_passenger_name">
<label class="control-label requiredField" for=
"id_passenger_name">Firstname:<span class=
"asteriskField">*</span></label>
<div class="controls">
<input class="textinput textInput" id="id_passenger_name"
maxlength="80" name="passenger_name" type="text">
</div>
</div>
</div>
<div class="col-md-6">
<div class="control-group" id="div_id_passenger_lastname">
<label class="control-label requiredField" for=
"id_passenger_lastname">Lastname:<span class=
"asteriskField">*</span></label>
<div class="controls">
<input class="textinput textInput" id=
"id_passenger_lastname" maxlength="80" name=
"passenger_lastname" type="text">
</div>
</div>
</div>
</div>
<div class="form-actions">
<input class="btn btn-primary" id="submit-id-submit" name="submit"
type="submit" value="Submit">
</div>
</form>
问题
我希望 input
字段自动添加 form-control
class。
field_class
属性 仅在您使用 bootstrap3
模板包(而不是默认的 bootstrap
包)时才有效。检查您是否定义了以下设置:
CRISPY_TEMPLATE_PACK = 'bootstrap3'
事实上,一旦你重新设置正确的模板,它会默认插入一个 form-control
class,所以你根本不需要手动设置它。
我正在使用 crispy-forms
进行简单构建,但是 field-class
属性没有按预期工作。
表格
class ArrivalForm(forms.Form):
def __init__(self, *args, **kwargs):
super(ArrivalForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'arrival-form'
self.helper.form_class = ''
self.helper.form_method = 'POST'
self.helper.form_action = ''
self.helper.field_class = 'form-control'
self.helper.layout = Layout(
Div(
Div('passenger_name', css_class='col-md-6'),
Div('passenger_lastname', css_class='col-md-6'),
css_class='row'),
)
self.helper.add_input(Submit('submit', 'Submit'))
passenger_name = forms.CharField(
label = "Firstname:",
max_length = 80,
required = True,
)
passenger_lastname = forms.CharField(
label = "Lastname:",
max_length = 80,
required = True,
)
渲染HTML
<form id="arrival-form" method="post" name="arrival-form">
<input name="csrfmiddlewaretoken" type="hidden" value=
"akxd0BVQrwaHbHr4FLjaDLz72BUUN9rQ">
<div class="row">
<div class="col-md-6">
<div class="control-group" id="div_id_passenger_name">
<label class="control-label requiredField" for=
"id_passenger_name">Firstname:<span class=
"asteriskField">*</span></label>
<div class="controls">
<input class="textinput textInput" id="id_passenger_name"
maxlength="80" name="passenger_name" type="text">
</div>
</div>
</div>
<div class="col-md-6">
<div class="control-group" id="div_id_passenger_lastname">
<label class="control-label requiredField" for=
"id_passenger_lastname">Lastname:<span class=
"asteriskField">*</span></label>
<div class="controls">
<input class="textinput textInput" id=
"id_passenger_lastname" maxlength="80" name=
"passenger_lastname" type="text">
</div>
</div>
</div>
</div>
<div class="form-actions">
<input class="btn btn-primary" id="submit-id-submit" name="submit"
type="submit" value="Submit">
</div>
</form>
问题
我希望 input
字段自动添加 form-control
class。
field_class
属性 仅在您使用 bootstrap3
模板包(而不是默认的 bootstrap
包)时才有效。检查您是否定义了以下设置:
CRISPY_TEMPLATE_PACK = 'bootstrap3'
事实上,一旦你重新设置正确的模板,它会默认插入一个 form-control
class,所以你根本不需要手动设置它。