简体   繁体   English

Django javascript不起作用$未定义

[英]Django javascript not working $ is not defined

So, I've been working on a django project (using djagno 1.11) and I saw a really cool chat feature that someone mocked up (using django 2.0) here with their git repo here . 所以,我一直工作在Django项目(使用djagno 1.11),我看到了一个非常酷的聊天功能,有人嘲笑了(使用Django 2.0) 在这里与他们的混帐回购协议在这里 I can recreate the entire project following the guide in its own app, but when I add it to my own I get into trouble. 我可以按照指南在自己的应用程序中重新创建整个项目,但是当我将其添加到自己的应用程序时,就会遇到麻烦。 My current app uses bootstrap and has javascript to add items to cart and other such things and I wonder if that is getting in the way. 我当前的应用程序使用bootstrap并具有javascript来将商品添加到购物车和其他此类物品,我想知道这是否会妨碍您。 That other javascript still works, the new javascript doesnt'. 该其他javascript仍然有效,新的javascript无效。 I get your basic error: 我得到您的基本错误:

Uncaught ReferenceError: $ is not defined 未捕获的ReferenceError:未定义$

and when I google search it I see that this means that something is being used before it is defined. 当我用google搜索它时,我看到这意味着在定义之前已经使用了某些东西。 I'm decent at python, but an extreme novice at javascript and so I'm not sure what is happening. 我在python上很不错,但是在javascript上却是个新手,所以我不确定会发生什么。 Diving into the error, I see the following: 深入探讨错误,我看到以下内容:

    <script src="https://project.s3.amazonaws.com/static/js/chat.js"></script>
<script>
    // For receiving
    sender_id = "";
    receiver_id = "1";

    //For sending
    $(function () {      <<--  This is where the error is pointing to.
        scrolltoend();
        $('#chat-box').on('submit', function (event) {
            event.preventDefault();
            var message = $('#id_message');
            send('', '', message.val());
            message.val('');
        })
    })
</script>

</div>

The above code is at the bottom of an html file that lives in the project/chat/templates/chat/chat.html and it references chat.js via {% static 'js/chat.js' %} which above is the aws line of code which lives in the cloudhere: project/static_my_proj/js/chat.js. 上面的代码在位于project / chat / templates / chat / chat.html中的html文件的底部,并且它通过{% static 'js/chat.js' %}引用了chat.js,上面是aws驻留在云中的代码行:project / static_my_proj / js / chat.js。 This code i'm integrating has stuff like 'materialize.js' and 'materialize.min.js' which I trying to understand what they do. 我正在集成的代码中有一些诸如“ materialize.js”和“ materialize.min.js”之类的东西,我试图了解它们的作用。

I wonder if I'm trying to combine two types of javascript. 我想知道是否要结合两种类型的javascript。 I'm completely lost. 我完全迷路了。 Does anyone have any pointers? 有人有指针吗? Is there any code I can provide that would help? 有什么我可以提供的代码会有所帮助吗?

Thanks 谢谢

You must add the following line 您必须添加以下行

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

above / before 高于/之前

<script src="https://project.s3.amazonaws.com/static/js/chat.js"></script>

this line 这条线

I think you didn't declare the variables correctly. 我认为您没有正确声明变量。

Variables, 变量

sender_id = "";
receiver_id = "1";

should be declared like this, 应该这样声明

var sender_id = "";
var receiver_id = "1";

// if you are using ES6 then

const sender_id="";
const receiver_id = "1";

Other than this, maybe you should include jquery as Ahmet Zeybek said. 除此之外,也许您应该像Ahmet Zeybek所说的那样包含jquery。

Materialize.js and Materialize.css is the part of the materialize framework which is similar to bootstrap. Materialize.js和Materialize.css是materialize框架的一部分,类似于引导程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM