How to use the bootstrap-datepicker in Django app?

前端 未结 5 2212
北荒
北荒 2020-12-19 03:17

I want to use the bootstrap-datepicker (https://bootstrap-datepicker.readthedocs.org) in my django application. I use Django 1.7.

In index.html file I have:

5条回答
  •  甜味超标
    2020-12-19 03:43

    For DJango version 2.1, 2.0, 1.11, 1.10 and 1.8

    To use Bootstrap date-picker in your Django app install django-bootstrap-datepicker-plus, follow the installation instructions on the GitHub page to configure it, then you can use it in Custom Forms and Model Forms as below.

    Usage in Custom Forms:

    from bootstrap_datepicker_plus import DatePickerInput
    
    class ToDoForm(forms.Form):
        user = forms.CharField()
        date_from = forms.DateField(widget = DatePickerInput())
    

    Usage in Model Forms:

    from bootstrap_datepicker_plus import DatePickerInput
    
    class MyForm(forms.ModelForm):
        class Meta:
            model = MyModel
            fields = ['user', 'date_from', 'date_to']
            widgets = {
                'date_form': DatePickerInput(),
                'date_to': DatePickerInput()
            }
    

    Usage with django-filters:

    from bootstrap_datepicker_plus import DatePickerInput
    
    class Filter(django_filters.FilterSet):
       class Meta:
            model = MyModel
            fields = ['user', 'date_from', 'date_to']
            widgets = {'date': DatePickerInput()}
    

    Disclaimer: This django package is maintained by me. For any issues with it please open issues on the Github Page instead of putting comments here.

提交回复
热议问题