javascript - Django and date range picker component for Twitter Bootstrap - Stack Overflow

I want to use this daterange picker for my Django ProjectThe problem is that I don't know what I h

I want to use this daterange picker for my Django Project

The problem is that I don't know what I have to do on the server side (Django)?

May be create some kind of widget?

May be subclass widgets.MultiWidget?

I think that this widget have to behave like two DateTimeFields?

Is this possible with Django?

I want to use this daterange picker for my Django Project

The problem is that I don't know what I have to do on the server side (Django)?

May be create some kind of widget?

May be subclass widgets.MultiWidget?

I think that this widget have to behave like two DateTimeFields?

Is this possible with Django?

Share Improve this question asked Mar 20, 2013 at 8:09 Julian PopovJulian Popov 17.5k12 gold badges57 silver badges87 bronze badges 1
  • you just have to define the id in your forms – catherine Commented Mar 20, 2013 at 8:38
Add a ment  | 

2 Answers 2

Reset to default 8

I had to do this; so I share the code here. It basically re-uses Django DateField.

class DateRangeField(forms.DateField):
    def to_python(self, value):
        values = value.split(' - ')
        from_date = super(DateRangeField, self).to_python(values[0])
        to_date = super(DateRangeField, self).to_python(values[1])
        return from_date, to_date

and

date_range = DateRangeField(required=False,
                            widget=forms.TextInput(attrs={'placeholder': _('from'),
                                                          'class': 'form-control datepicker'}))

Given that the example output is

MM/DD/YYYY - MM/DD/YYYY

This is what will post to your view. I would just handle this on your own rather than using a Django Form, and store it in two DateFields. Something along the lines of this:

date_range = request.POST['date_range']
start_date_string = date_range.split(' - ')[0]
end_date_string = date.range.split(' - ')[1]

This will get you your two strings needed, then simply just pass them into the DateFields and save it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742369164a4430910.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信