如何使用jQuery UI Datepicker作为Django小部件? [英] How to use jQuery UI Datepicker as a Django Widget?

查看:193
本文介绍了如何使用jQuery UI Datepicker作为Django小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一些Django 1.3模型有DateField属性。当生成表单时,我想使用jQuery UI Datepicker而不是纯文本字段。我明白我可以创建新的小部件,但我不明白如何。另外,我不知道Django是否已经完成了这样的操作(我已经没有机会了)。

Some of my Django 1.3 models have DateField properties. When a form is generated, I'd like to use the jQuery UI Datepicker instead of a plain text field. I understand that I could create new widgets but I don't understand how. In addition, I am not sure if anything like this has already been done for Django (I googled it, no chance).

如何创建一个可以使用的Django Widget jQuery UI有一个非常好的日期UI选择器,可以在多个模型(和页面)上重用jQuery UI Datepicker?

How to create a Django Widget that I can reuse across multiple models (and page) for the jQuery UI Datepicker?

推荐答案


您可以在此处获取: http://jqueryui.com

例如,你有如下形式:

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

从这里你想绑定JQuery日期小部件到你的字段在你的模板内

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

我假设在这里你将 DateForm 传递给你的模板,你的JQuery路径是正确的。

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>

请注意, myDate 之前是 ID _ 。 Django透明地这样做,所以确保你这样匹配: id_myDate

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

希望这可以帮助你。

这篇关于如何使用jQuery UI Datepicker作为Django小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆