How to use the bootstrap-datepicker in Django app?

前端 未结 5 2220
北荒
北荒 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:48

    Update: Warning, this suggestion uses the dateTIMEpicker instead of datepicker. I suggested this because you don't have to use the time functionality of the datetimepicker, so it was quite handy in my case.

    Since I have been trying to get datetimepicker to work (in combination with a model form) for quite some time, I thought I would post the solution, that feels like an accumulation of all stackoverflow posts on this topic ;)

    So, first of all, make sure to also include moment.js, as it is required by bootstrap-datetimepicker (see this stackoverflow post). The order is important, see the mentioned post.

    Then use this site to get the type of datetimepicker you need. If you have no model form in which you want to embed the datetimepicker, you should be fine with just copying that into your html code.

    If you want to make one of your model forms field a fancy datepicker (without time) field like I did, then do the following:

    base.html

    
    
    
    

    forms.py

    class MyForm(forms.ModelForm):
        class Meta:
        ...
        widgets = {'myDateField': forms.DateInput(attrs={'id': 'datetimepicker12'})}
        ...
    

    myForm.html:

    ... {% bootstrap_form form %} ...

    I hope I was able to save you some time :)

提交回复
热议问题