Django 1.8 & Django Crispy Forms: Is there a simple, easy way of implementing a Date Picker?

前端 未结 4 2005
逝去的感伤
逝去的感伤 2021-02-07 13:41

There are an awful lot of date/datetime picker implementations out there. Are there any that integrate with Django and Crispy Forms particularly well, and how are they used?

4条回答
  •  北海茫月
    2021-02-07 14:02

    My solution is simple and uses the datetimepicker from https://eonasdan.github.io/bootstrap-datetimepicker/.

    Include the css file:

    
    

    Include the js files:

    
    
    
    
    

    Define a css class for the field in the form (works with Crispy too):

    class MyForm(forms.ModelForm):
        class Meta:
            model = MyModelo
            fields =       ['a','b','c']
    
        def __init__(self, *args, **kwargs):
            super(MyForm, self).__init__(*args, **kwargs)
            self.fields['a'].widget.attrs['class'] = 'datepicker'
    

    In the template include a JQuery function linking to the css class:

    
    

    This solution works great with Django 1.10, Crispy Forms and Django Model Forms.

提交回复
热议问题