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

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

问题描述

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

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

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

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

希望得到您的帮助

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 错误.

库在 base.html 中

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

解决方案

Django 模板引擎已经提供了一个标签来继承你的 HTML 结构,称为extend".

<块引用>

标签 extends" 是用于扩展父模板.

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

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

{% 加载静态文件 %}<html lang="en"><head><title>Hello World</title></head><身体><div id="内容">{% 块内容 %}{% 端块 %}

{% 块脚本 %}<script src="{% static 'js/main.js' %}"></script>{% 结束块 %}

并且您有另一个名为 milk.html 的 HTML,您需要与 base.html 相同的所有内容,但包括 milk.js,您只需执行以下操作即可.>

{% 加载静态文件 %}{% 扩展base.html"%}{% 块脚本 %}<!-- block.super 将从父模板中获取块的内容-->{{ block.super }}<script src="{% static 'js/milk.js' %}"></script>{% 结束块 %}

阅读有关文档的更多信息[1]:[1]:https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#std:templatetag-extends

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..

I tried adding as a normal js file

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

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

I hope your help

EDITED:

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.

The libraries are in base.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 template engine has already provided a tag for inherit your HTML structure called 'extend'.

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

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

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>

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 %}

Read more about docs[1]: [1]: https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#std:templatetag-extends

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

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