【问题标题】:getting Jquery's datepicker to work with Django DateField form让 Jquery 的 datepicker 与 Django DateField 表单一起工作
【发布时间】:2015-12-26 04:47:16
【问题描述】:

我正在关注http://hexonio.com/blog/2012/03/07/calendar-picker-django-forms/ 上有关使用 jquery datepicker 的教程。

我的表单是一个简单的 DateField:

class CalendarForm(forms.Form):
    field1 = forms.DateField()

我按照说明安装了日期选择器:

   ...
    $(function() {
        $( "id_field1" ).datepicker();
    });

</script>

教程说要在本地安装 jquery,所以我从http://jqueryui.com/download 中选择了 1.11.14,因为它说“稳定”,并将其解压缩到媒体中的 jquery 文件夹:

cchilders@cody_pc:~/projects/django_projects/bookmarks_tracker/media/jquery/jquery-ui-1.11.4.custom$ ls
external    jquery-ui.css      jquery-ui.min.js             jquery-ui.theme.css
images      jquery-ui.js       jquery-ui.structure.css      jquery-ui.theme.min.css
index.html  jquery-ui.min.css  jquery-ui.structure.min.css

为了安全起见,我将它们全部安装在“base.html”中:

base.html:

<!DOCTYPE html>
<html lang="en">
    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Sitename</title>

        <link type="text/css" href="/media/jquery/jquery-ui-1.11.4.custom/jquery-ui.css" rel="Stylesheet" />
        <link type="text/css" href="/media/jquery/jquery-ui-1.11.4.custom/jquery-ui.min.css" rel="Stylesheet" />
        <link type="text/css" href="/media/jquery/jquery-ui-1.11.4.custom/jquery-ui.theme.css" rel="Stylesheet" />
        <link type="text/css" href="/media/jquery/jquery-ui-1.11.4.custom/jquery-ui.theme.min.css" rel="Stylesheet" />
        <link type="text/css" href="/media/jquery/jquery-ui-1.11.4.custom/jquery-ui.structure.css" rel="Stylesheet" />
        <link type="text/css" href="/media/jquery/jquery-ui-1.11.4.custom/jquery-ui.structure.min.css" rel="Stylesheet" />
        <script type="text/javascript" src="/media/jquery/jquery-ui-1.11.4.custom/jquery-ui.js"></script>
        <script type="text/javascript" src="/media/jquery/jquery-ui-1.11.4.custom/jquery-ui.min.js"></script>

        <!-- Load javascript libraries -->
        <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script> 

        <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.css" rel="stylesheet">
        <link href="/media/css/main.css" rel="stylesheet" type="text/css" />

        <link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
        <!-- <link href="static/select2/css/select2.min.css" rel="stylesheet" />
        <script src="static/select2/js/select2.min.js"></script> -->

        <!-- semantic -->
        <link href="https://cdnjs.com/libraries/semantic-ui" rel="stylesheet"/>

我已将那里的所有文件安装到 base 中,但 datepicker 不起作用。当我注释掉 &lt;script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; 时,页面上根本没有 jquery 起作用。所以我解压缩的 jquery 不起作用,但我认为它找到了带有 304 重定向的文件:

127.0.0.1 - - [28/Sep/2015 16:32:21] "GET /silo/ HTTP/1.1" 200 -
127.0.0.1 - - [28/Sep/2015 16:32:21] "GET /media/jquery/jquery-ui-1.11.4.custom/jquery-ui.css HTTP/1.1" 304 -
127.0.0.1 - - [28/Sep/2015 16:32:21] "GET /media/jquery/jquery-ui-1.11.4.custom/jquery-ui.min.css HTTP/1.1" 304 -
127.0.0.1 - - [28/Sep/2015 16:32:21] "GET /media/jquery/jquery-ui-1.11.4.custom/jquery-ui.theme.css HTTP/1.1" 304 -
127.0.0.1 - - [28/Sep/2015 16:32:21] "GET /media/jquery/jquery-ui-1.11.4.custom/jquery-ui.theme.min.css HTTP/1.1" 304 -
127.0.0.1 - - [28/Sep/2015 16:32:21] "GET /media/jquery/jquery-ui-1.11.4.custom/jquery-ui.structure.css HTTP/1.1" 304 -
127.0.0.1 - - [28/Sep/2015 16:32:21] "GET /media/jquery/jquery-ui-1.11.4.custom/jquery-ui.structure.min.css HTTP/1.1" 304 -
127.0.0.1 - - [28/Sep/2015 16:32:21] "GET /media/jquery/jquery-ui-1.11.4.custom/jquery-ui.js HTTP/1.1" 304 -
127.0.0.1 - - [28/Sep/2015 16:32:21] "GET /media/css/main.css HTTP/1.1" 304 -
127.0.0.1 - - [28/Sep/2015 16:32:21] "GET /media/jquery/jquery-ui-1.11.4.custom/jquery-ui.min.js HTTP/1.1" 304 -

模板中的表单是calendar_form...

silo.html:

{% if articles %}

        <div style="width:94%;">
            {% if tags %}
                <div class="help-text">
                    <p>You can filter your articles by tags. Articles with the most tags matched come first, and all articles that have at least one tag will show up.</p>
                </div>

                <div>
                    <h4>Tags</h4>
                    <form id="category_form" method="post" action="{% url 'index:silo' %}" enctype="multipart/form-data">
                          {% csrf_token %}

                          <section id="multiple" class="row">
                              <div class="col-md-8">
                                  <span>
                                      <select  style="width:90%;" multiple="multiple" id="selected_tags" name="selected_tags">
                                          {% for tag in tags %}        
                                              <option value="{{tag.slug}}">{{tag.name}}</option>
                                          {% endfor %}
                                      </select>                
                                  </span>
                               </div>
                          </section>
                          <br/>
                          {{ calendar_form }}
                          <input type="submit" name="submit" value="Search"/>
                          <br/>
                    </form>
                    <br/>
                </div>
            {% endif %}

我检查了它在 jquery 中注册为id_field1 是正确的,它是id_field1。

令人困惑的部分是我的其他 jquery 项目是如何工作的(切换、交换类、交换文本、ajax 请求、select2 等等),但这个 datepicker 没有。

我怎样才能让这个日期选择器像日历一样工作,你可以像往常一样滚动浏览和选择日期,使用内联或外部链接的 jquery?

谢谢

此控制台错误如下:

未捕获的 ReferenceError:未定义 jQuery 来自我的 jquery-ui.js

未捕获的 ReferenceError:未定义 jQuery 来自我的 jquery-ui.min.js

未捕获的类型错误:$(...).datepicker 不是访问页面中的函数 (http://127.0.0.1:8000/silo/)

如果我注释掉本地 jquery 文件并保留 cloudfare 文件,它仍然显示“datepicker 不是函数”,但文档说它是 http://api.jqueryui.com/datepicker/

放置

<script type="text/javascript" src="/media/jquery/jquery-ui-1.11.4.custom/jquery-ui.min.js"></script> 
        <script type="text/javascript" src="/media/jquery/jquery-ui-1.11.4.custom/jquery-ui.js"></script>

在 CSS 导致大量问题之前的顶部:

【问题讨论】:

  • 先加载jquery.min.js,再加载插件库。

标签: javascript jquery django jquery-ui datepicker


【解决方案1】:

您遇到控制台错误是因为您在 jquery-ui.min.js 之后加载 jquery.min.js。如果您交换订单,它应该可以工作。一般来说,由于很多插件都依赖于 jquery,所以你应该把它放在你的 JS 声明的顶部。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 2010-12-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多