简体   繁体   English

在一个Django项目中包含一个javascript文件中的javascript个文件

[英]Include javascript files in a javascript file in a Django project

I'm using the chart library apexcharts and defined a javascript function for a chart that I'm using multiple times on different parts of the website.我正在使用图表库 apexcharts 并为我在网站的不同部分多次使用的图表定义了 8822996504788 function。 The function needs some other scripts to work. function 需要一些其他脚本才能工作。 So at the moment, everytime I want to use that function (located in util.js), I have to import the scripts like this in my html template:所以目前,每次我想使用 function(位于 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>

How can I include the first 3 in my util.js, so that I just have to import util.js?如何将前 3 个包含在我的 util.js 中,以便我只需要导入 util.js?

For importing resources for multiple templates without repeating it's better to have a parent base.html that handle it:为了在不重复的情况下导入多个模板的资源,最好有一个父 base.html 来处理它:

base.html:基地.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: user_page.html:

{% extends "base.html" %} # extends will add the scripts in header

{% block content %}
   # your content here
{% endblock %}

For more, you can read over it at the document: https://docs.djangoproject.com/en/3.1/ref/templates/language/#templates有关更多信息,您可以在文档中阅读它: https://docs.djangoproject.com/en/3.1/ref/templates/language/#templates

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

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