如何使用crispy表单在一行上显示Django SelectDateWidget

时间:2014-05-29 16:18:28

标签: django django-forms django-crispy-forms django-widget

我试图在一行上显示使用Django SelectDateWidget渲染出的3个选择字段。当我使用酥脆的表格时,它们都在不同的行上。有没有办法使用Layout帮助器来实现这个目标?

谢谢!

class WineAddForm(forms.ModelForm):
hold_until = forms.DateField(widget=SelectDateWidget(years=range(1950, datetime.date.today().year+50)), required=False)
drink_before = forms.DateField(widget=SelectDateWidget(years=range(1950, datetime.date.today().year+50)), required=False)

helper = FormHelper()
helper.form_method = 'POST'
helper.form_class = 'form-horizontal'
helper.label_class = 'col-lg-2'
helper.field_class = 'col-lg-8'
helper.add_input(Submit('submit', 'Submit', css_class='btn-wine'))

helper.layout = Layout(
    'name',
    'year',
    'description',
    'country',
    'region',
    'sub_region',
    'appellation',
    'wine_type',
    'producer',
    'varietal',
    'label_pic',
    'hold_until',
    'drink_before',
)

class Meta:
    model = wine
    exclude = ('user', 'slug', 'likes')

2 个答案:

答案 0 :(得分:7)

将其添加到SelectDateWidget字段的布局中:

MultiWidgetField('field_name', attrs=({'style': 'width: 33%; display: inline-block;'}))

答案 1 :(得分:1)

您可以像这样更改三个小部件的宽度:

.selectdatewidget {
    display: inline-block;
}

select[name$="_day"].selectdatewidget {
    width: 27%;
    color: red;
}
select[name$="_month"].selectdatewidget {
    width: 38%;
    color: green;
}
select[name$="_year"].selectdatewidget {
    width: 35%;
    color: blue;
}

SelectDateWidget with three differents width