在Django中的嵌套包含模板中运行的Javascript

时间:2018-12-03 22:42:43

标签: javascript django django-templates

如何获取3级模板以在1级/基本级JS中运行JS?

base.html
    <html>
      <head>
        {% block inline-scripts %}{% endblock inline-scripts %}
      </head>
      <body>
        {% block content %} Default Content{% endblock content %}
      <body>
    </html>

level1.html
    {% extends "base.html" %}
    {% load static %}
    {% block inline-scripts %}
      {% this will get loaded %}
      <script src='some/file.js'></script>
    {% endblock inline-scripts %}
    {% block content %} 
      Level 1 Content 
      {% include 'level2.html' with whatever=variables %}       
    {% endblock content %}

level2.html
    {% load static %}
    Level 2 HTML
    {% include 'level3.html' with whatever=variables %}       

level3.html
    {% load static %}
    Level 3 HTML - How do I get the JS here to have the scope of the level 1/base
    {% block inline-scripts %} 
      {% this will NOT get loaded %}
      $(document).ready(function() {
        //Current error
        //Uncaught ReferenceError: $ is not defined
      })
    {% endblock inline-scripts %} 
    <script>
      manuallyEnteringJS.doesntWorkEither
    </script>

0 个答案:

没有答案