How to display Django SelectDateWidget on one line using crispy forms

亡梦爱人 提交于 2019-12-10 14:08:40

问题


I am trying to display the 3 select fields that are rendered out using Django SelectDateWidget on one line. When I use crispy forms, they are all on separate rows. Is there a way to use the Layout helper to achieve this?

Thank you!

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')

回答1:


Add this to your layout for your SelectDateWidget fields:

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



回答2:


You can change the width for each of the three widget like this :

.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



来源:https://stackoverflow.com/questions/23938003/how-to-display-django-selectdatewidget-on-one-line-using-crispy-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!