简体   繁体   中英

Javascript modules not available templates not inherited from base Django tempate

I have this issue hope you have some idea how to solve it. In a Django templates which is not a "child" template of base template, I would like to use javascript modules which are in the base template, ie Jquery. Is there a way to achieve this without load again all javascript libraries I need?

Lets say base.html

{% block js %}  
<script src="some/path/to/jquery.js"></script>
{% endblock $}

some_component_base_template.html

// Not inherited from base.html

{% block somejsblock %}
<script>
$(document).ready(function(
   //some code I need
))
</script>
{% endblock %}

This block of html can be inserted anywhere in the template by user via slots mechanisms, this is why it doesn't inherit from base.html, if I create another header, every time that this block of html is loaded, javascripts are going to be loaded too.

The template should inherit from base, then. If there's stuff currently in base you don't want to be in some other templates, you should use an intermediate parent which itself inherits from base and which some templates can use and others don't.

如果您不是从基础继承,则应创建一个头并在其中手动添加引用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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