【问题标题】:Django datepicker not working?Django 日期选择器不工作?
【发布时间】:2017-05-02 21:38:29
【问题描述】:

我正在尝试在用户个人资料页面中实现日期选择器,但是当我单击文本字段时它没有显示日期选择器。有什么想法吗?

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ('birth_date', )
        widgets = {
            'birth_date': forms.DateInput(attrs={'class': 'form-control'}),
        }

【问题讨论】:

    标签: python html django forms datepicker


    【解决方案1】:

    在您的模板中安装 datapicker 库和 jquery-ui https://jqueryui.com/datepicker/

    <script type="text/javascript">
        $(function(){
            $( "#id_birth_date" ).datepicker({
              yearRange: "-120:+0",
              changeMonth: true,
              changeYear: true,
              dateFormat: "yy-mm-dd",
              beforeShow: function() {
              setTimeout(function(){
                $('.ui-datepicker').css('z-index', 9999);
            }, 0);
        }});
      });
    </script>
    

    【讨论】:

    【解决方案2】:

    假设 django 默认为 DateField 提供日期选择器。问题可能是当您添加小部件以添加类 form-control 时,它会重置 django 应用的所有默认类。 尝试删除您的课程

    thispost 可能会解决您的问题

    【讨论】:

    • 你的意思是删除 attrs={'class': 'form-control'}?我试过了,还是不行。
    • @arcade16 你确定 django 默认为其 DateInput 字段提供一个日期选择器
    猜你喜欢
    • 2014-07-21
    • 2017-12-24
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多