我在Django项目中执行Javascript代码时遇到麻烦。
下面的操作很好--即不使用模板继承
HTML:
<!DOCTYPE html>
<html>
<head>
{% load static %}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="{% static 'ads/test.js' %}" type="text/javascript"></script>
<title> Test </title>
</head>
<body>
<form action="{% url 'test' %}" method="post" id="my_form">
{% csrf_token %}
<button type="submit" class="btn btn-success">Submit</button>
</form>
</body>
</html>
test.js
$(document).ready(function() {
$("#my_form").submit(function(){
alert('submitted');
});
});
由于上面的外部Javascript文件test.js已经成功执行,所以我假设静态url、静态DIR等的设置是正确的。还有,既然jquery已经起作用了,我假设先jquery再js代码的顺序也是正确的。
当我使用模板继承时,问题就来了。
基本HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
{% block scripts %}
{% endblock %}
<title> {% block title %}{% endblock %} </title>
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
继承的模板:
{% extends "base.html" %}
{% block scripts %}
{% load static %}
<script src="{% static 'ads/test2.js' %}" type="text/javascript"></script>
{% endblock %}
{% block title %}
Test
{% endblock %}
{% block body %}
<form action="{% url 'test' %}" method="post" id="my_form">
{% csrf_token %}
<button type="submit" class="btn btn-success">Submit</button>
</form>
{% endblock %}
但是,上面的操作不起作用,即提交按钮时没有发生任何事情。唯一的改变是模板继承--没有它,js代码可以工作,但是如果包含它,就会出问题。
在chrome-inspect-network中,没有加载test.js文件,但可以看到bootstrap、popper、jquery js文件已经加载。Chrome控制台显示无错误
我扩展基模板或继承基模板的方式有问题吗?
我认为您应该在扩展后在顶部加载static,而不是在block内部加载static。那么,试试这样的方法:
{% extends "base.html" %}
// My change
{% load static %}
{% block scripts %}
// Changed from
<script src="{% static 'ads/test2.js' %}" type="text/javascript"></script>
{% endblock %}
{% block title %}Test{% endblock %}
{% block body %}
<form action="{% url 'test' %}" method="post" id="my_form">
{% csrf_token %}
<button type="submit" class="btn btn-success">Submit</button>
</form>
{% endblock %}