如何在Django中组织JS文件? [英] how to organize JS files in Django?

查看:134
本文介绍了如何在Django中组织JS文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Django项目,我包含不同的库JS和我创建JS文件来管理其他库,但我不知道每个html页面的JS文件的正确组织,例如,我有基本模板中的Main.js和Milk.js,但我不希望在同一个基本模板中同时拥有这两个文件,我希望每个页面都有单独的文件..

I am currently working with a Django project, I include different libraries JS and I create JS files for manage the other libraries, but I don't know the correct organization of JS files for each html page, for example, I have a "Main.js" and "Milk.js" in base template but I don't want have both files in the same base template, I want separate files for each page..

我尝试添加为普通的js文件

I tried adding as a normal js file

<script src="{{ STATIC_URL }}js/milk.js"></script>

但它向我显示一条错误消息,询问我从base.html继承的几个依赖项

But it show me a error message asking me several dependencies when inherited from base.html

我希望得到你的帮助

编辑:

Cuando heaadadias en mis archivos de plantillas,sin mostrarme error en la consola de cromo pero en la consola django mostrarme los archivos JS de carga con 304 error。

Cuando he añadido en mis archivos de plantillas, sin mostrarme error en la consola de cromo pero en la consola django mostrarme los archivos JS de carga con 304 error.

图书馆位于base.html

The libraries are in base.html

奇怪的是,当我从home.html点击时我可以加载milk.js但是当我点击其他页面例如cow.html从Milk.html没有加载js文件时,即使我做了同样的事情milk.html。

it's strange, I can load milk.js when I click from home.html but when I will click in other page for example "cow.html" from "Milk.html" no load js file even when I did the same as "milk.html".

推荐答案

Django模板引擎已经提供了一个用于继承HTML结构的标签,称为extend。 / p>

Django template engine has already provided a tag for inherit your HTML structure called 'extend'.


标记 extends 是用于扩展父模板。

Tag "extends" is a using for extends a parent template.

{%extendsbase.html%}使用文字值base.html作为要扩展的父模板的名称。

{% extends "base.html" %} uses the literal value "base.html" as the name of the parent template to extend.

base.html 是可以扩展的父模板。

base.html is the parent template that can extendable.

{% load staticfiles %}
<html lang="en">
    <head><title>Hello World</title></head>
    <body>
        <div id="content">
            {% block content %}{% endblock %}
        </div>

        {% block scripts %}
        <script src="{% static 'js/main.js' %}"></script>
        {% endblock %}

    </body>
</html>

您还有另一个名为 milk.html 的HTML,您需要所有相同的内容作为base.html但包括 milk.js 你只需要这样做。

and you have another HTML called milk.html that you need everything same as the base.html but include milk.js you just do something like this.

{% load staticfiles %}
{% extends "base.html" %}

{% block scripts %}
    <!-- block.super will get the content of the block from the parent template -->
    {{ block.super }}
    <script src="{% static 'js/milk.js' %}"></script>
{% endblock %}

了解更多关于'extends'的信息: https://docs.djangoproject.com/en/1.9/ref/templates / builtins / #std:templatetag-extends

Read more about 'extends' : https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#std:templatetag-extends

这篇关于如何在Django中组织JS文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆