简体   繁体   English

JavaScript 不在 Django 中运行

[英]JavaScript doesn't run in Django

I'm trying to find out how to run JavaScript in Django to create chained forms.我试图找出如何在Django运行JavaScript来创建链式表单。 But at the first I want to find out how to even run JavaScript .但一开始我想知道如何运行JavaScript I've created a simple main.js file which is in static forder.我创建了一个简单的main.js文件,它是static文件。

I've added a path to main.js into the head of html .我已将main.js的路径添加到html的头部。 And the script have to run when the page is loaded (just to be sure that I can step forward).并且脚本必须在页面加载时运行(只是为了确保我可以向前迈进)。

I've put alert on the beginning of the function so I can see whether the js has been run.我在函数的开头加上了alert ,所以我可以看到js是否已经运行。 But I can't see no alert nor js in chrome inspect .但是我在chrome inspect中看不到alertjs

Could you guys tell me where is the problem?大家能告诉我问题出在哪里吗?

main.js主文件

$(document).ready(function(){
    alert("OK")
    $.ajax({
        url: "get-possible-levels/",
        type: "POST",
        data: {language: $('#id_language').val()},
    })
})

template:模板:

{% extends 'base.html' %}
{% load static %}
{% block head %}
    <script src="{% static  "js/main.js" %}"></script>
{% endblock %}
{% block content %}
    {% if user.is_authenticated %}
        <form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
            {{ language_form }}
            <button value="Update" type="submit">Submit</button>
        </form>
    {% endif %}
{% endblock %}

View:看法:

@login_required                                                                                      
def create_order(request):                                                                           
    language_form = LanguageLevelForm(request.POST or None)                                                                                                                                    
    return render(request,'auth/jobs/create-job-test.html',context={'language_form':language_form})  

EDIT: The main.js seems to be executed but it does not alert anything.编辑: main.js似乎已执行,但它没有main.js任何alert I've checked inspect (and I've tried to put semicolon after alert('ok') ) :我已经检查过inspect (并且我已经尝试在alert('ok')之后加上分号):

在此处输入图片说明

Your {% load static %} should be {% load staticfiles %} .你的{% load static %}应该是{% load staticfiles %} After that try refreshing the page the way I described in my comment.之后尝试按照我在评论中描述的方式刷新页面。 BTW Are you actually including JQuery? BTW 你真的包括 JQuery 吗? Your question is about JavaScript.你的问题是关于 JavaScript 的。 You should try to use a vanilla JavaScript alert before adding JQuery code just to troubleshoot it.在添加 JQuery 代码之前,您应该尝试使用 vanilla JavaScript 警报来解决它。 If you can get that working try adding this to your <head>如果您可以使用它,请尝试将其添加到您的<head>

 <script src="jquery-1.12.0.min.js"></script>

Check if you have the path to the js file wrong.if your project folder has 2 folders inside (one with html files and 1 with Js files) then on your path with the ../ you go up one level on your folder and then go inside your javascript folder to find the file.检查你的 js 文件路径是否错误。如果你的项目文件夹里面有 2 个文件夹(一个是 html 文件,1 个是 Js 文件),然后在你的路径上用 ../ 你在你的文件夹上上一级,然后进入您的 javascript 文件夹以查找该文件。 So instead of所以代替

 <script src="{% static  "js/main.js" %}"></script>

write

 <script src="../js/main.js"></script>

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

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