javascript - 在 Django 项目的 javascript 文件中包含 javascript 文件
问题描述
我正在使用图表库 apexcharts 并为我在网站的不同部分多次使用的图表定义了一个 javascript 函数。该函数需要一些其他脚本才能工作。所以目前,每次我想使用该函数(位于 util.js 中)时,我都必须在我的 html 模板中导入这样的脚本:
<script type="application/javascript" src="{% static 'shared/js/apexcharts/apexcharts.min.js' %}"></script>
<script type="application/javascript" src="{% static "shared/js/apexcharts/dependency2.js" %}"></script>
<script type="application/javascript" src="{% static "shared/js/apexcharts/dependency1.js" %}"></script>
<script type="application/javascript" src="{% static "shared/js/apexcharts/util.js" %}"></script>
如何在我的 util.js 中包含前 3 个,这样我只需要导入 util.js?
解决方案
为了在不重复的情况下导入多个模板的资源,最好有一个父 base.html 来处理它:
base.html:
<html>
<head>
<script type="application/javascript" src="{% static 'shared/js/apexcharts/apexcharts.min.js' %}"></script>
<script type="application/javascript" src="{% static "shared/js/apexcharts/dependency2.js" %}"></script>
<script type="application/javascript" src="{% static "shared/js/apexcharts/dependency1.js" %}"></script>
<script type="application/javascript" src="{% static "shared/js/apexcharts/util.js" %}"></script>
</head>
<body>
{% block content %}
#your other small page here
{% endblock %}
</body>
</html>
user_page.html:
{% extends "base.html" %} # extends will add the scripts in header
{% block content %}
# your content here
{% endblock %}
有关更多信息,您可以在文档中阅读它:https ://docs.djangoproject.com/en/3.1/ref/templates/language/#templates
推荐阅读
- elasticsearch - Elasticsearch 根据主查询的结果查找文档
- python - 'numpy.ndarray' 对象在使用矩阵时不可调用
- python - 遵循 Python Lazy Logging Formatting,但 pylint 仍然显示(logging-not-lazy)消息
- javascript - 无法在 React 中读取 null 的属性“样式”
- javascript - 如何将嵌入的外部 svg 文件与视图框对齐
- python - 将 automap_base 与 Oracle db 一起使用时,SQLAlchemy 退出并出现错误“未识别类型 'LONG RAW'”
- kubernetes - 从 NodeJs 到 Application Insight 的连接问题
- browser - 在不刷新页面的情况下检测元掩码安装
- javascript - 如何允许在 Firebase 中访问您的网络应用程序的照片
- flutter - 如何将多个小部件添加到小部件列表