How to use jQuery UI Datepicker as a Django Widget?

亡梦爱人 提交于 2019-12-03 05:37:57
Dave

JQueryUI has a very good date UI picker. You can get it here: http://jqueryui.com

Say for example you have the following form:

class DateForm(forms.Form):
   myDate = forms.DateField()

From here you want to bind the JQuery date widget to your field from within your template.

I am assuming here you are passing the DateForm to your template and your path(s) to JQuery are correct.

<head>
    <link rel="stylesheet" href="/themes/base/jquery.ui.all.css">
    <script src="/jquery.js"></script>
    <script src="/ui/jquery.ui.core.js"></script>
    <script src="/ui/jquery.ui.widget.js"></script>
    <script src="/ui/jquery.ui.datepicker.js"></script>
    <script>
    $(function() {
        $( "#id_myDate" ).datepicker();
    });
    </script>
</head>
<body>

<p>Date: <input type="text" id="id_myDate"></p>

</body>

Please note that myDate is preceded by id_. Django does this transparently so make sure you match it as such: id_myDate.

Hope this helps you.

You should look on DateInput - you'll need to inherit your widget from it. Also look on admin widgets - there are some JS appended. So take DateInput, add some JS and you're done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!