efficiently loading jquery and javascript files in django templates

前端 未结 1 1491
挽巷
挽巷 2020-12-16 03:25

In one of my django projects i am using lots of custom jquery scripts and lots of open source jquery plugins. Now if i load all the jquery scripts in my base template, I wil

相关标签:
1条回答
  • 2020-12-16 03:52

    Define a block in your parent template where you include your "default" JavaScript files and then extend the block as needed:

    # base.html
    
    {% block js %}
        <script src="{{ STATIC_URL }}js/jquery.1.js"></script>
        <script src="{{ STATIC_URL }}js/jquery.2.js"></script>
        <script src="{{ STATIC_URL }}js/jquery.3.js"></script>
        <script src="{{ STATIC_URL }}js/jquery.4.js"></script>
        <script src="{{ STATIC_URL }}js/jquery.5.js"></script>
    {% endblock %}
    
    
    # child.html
    
    {% extends "base.html %}
    
    {% block js %}
        {{ block.super }} {# includes previous content in block #}
        {# view-specific imports here #}
    {% endblock %}
    

    This will prevent repetition in your templates. Check out: template inheritance for more information about templates and inheritance.

    You can use django-compressor to combine and minify CSS and JS imports and cache them for efficient loading.

    0 讨论(0)
提交回复
热议问题