datepicker 在 Django 中不起作用?

datepicker is not working in django?

base.html

中添加的脚本
$(function() {
  $('.datepicker').datepicker({
    format: 'mm/dd/yyyy',
    // startDate: '-3d'
    startDate: "2013-02-14 10:00",
    changeMonth: true,
    changeYear: true,
    yearRange: "1900:2012",
    uiLibrary: 'bootstrap4',
    autoclose: true,
  });
})

我的 class MessLeaveFormforms.py

class MessLeaveForm(forms.ModelForm):

    departure_date = forms.DateField()
    arrival_date = forms.DateField(widget=forms.DateInput(format='%d/%m/%Y'))
    departure_time = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))
    arrival_time = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))
    verification = forms.ChoiceField(choices=BOOL_VALUES,initial="Pending",widget=forms.HiddenInput(),required=False)
    approval = forms.ChoiceField(choices=BOOL_VALUES,initial="Pending",widget=forms.HiddenInput(),required=False)
    status = forms.ChoiceField(choices=BOOL_VALUES,initial="Pending",widget=forms.HiddenInput(),required=False)
    hostel_suscribed = forms.ChoiceField(choices = HOSTEL_CHOICES,required = True)
    mess_manager_doc = forms.FileField(required=False)
    faculty_doc = forms.FileField(required=False)

    class Meta:
        model = MessLeaveModel
        fields = ('idNo','hostel_suscribed','departure_date','departure_time','arrival_date','arrival_time','mess_manager_doc','faculty_doc','verification','approval','status')
        widgets = {
            'departure_date': forms.DateInput(attrs={'class':'datepicker'}),
        }

我的模型 MessLeaveModelmodels.py

class MessLeaveModel(models.Model):
    # user = models.ForeignKey(OccupantDetails,null=True,on_delete=models.CASCADE)
    idNo = models.ForeignKey(OccupantDetails,on_delete = models.CASCADE)
    username = models.CharField(max_length=255, null=False,blank=False,default="")
    hostel_suscribed = models.CharField(max_length=255,choices = HOSTEL_CHOICES,null=True)
    departure_date = models.DateField()
    arrival_date = models.DateField()
    departure_time = models.TimeField(null = True,blank = True)
    arrival_time = models.TimeField(null=True,blank=True)
    faculty_doc = models.FileField(upload_to='documents/',null=True,blank=True)
    mess_manager_doc = models.FileField(upload_to='documents/',null=True,blank=True)
    verification = models.CharField(max_length=255,choices=BOOL_VALUES,default="Pending")
    approval = models.CharField(max_length=255,choices=BOOL_VALUES,default="Pending")
    status = models.CharField(max_length=255,choices=BOOL_VALUES,default="Pending")
    comment = models.CharField(max_length=255,blank=True,null=True,default="")


    def __str__(self):
        return '%s_%s_%s' %(self.idNo,self.departure_date,self.arrival_date)

我的 Django HTML 出发日期

<label class="control-label col-sm-2"> Departure Date</label>
<div class="col-sm-10">{{form.departure_date}}

我正在尝试在 MessLeaveModel 模型的 departure_date 字段中添加 datepicker。我已经在 base.html 中添加了相关脚本,并通过 MessLeaveForm 的 meta class 中的小部件添加了这个 class。我在我的 messLeaveForm.html 中扩展了 base.html 并在那里接受输入。 datepicker 完全没有反应。

您可能需要查看 django-tempus-dominus 包,其中包含 Bootstrap 4 个 Django 日期选择器小部件:

https://pypi.org/project/django-tempus-dominus/

它是 jQuery Tempus Dominus 库的一组垫片,Bootstrap 日期选择器的完全重写后继者。祝你好运!