简体   繁体   中英

How to include html template into another html with javascript

I have an html page called main.html that is extended with base.html . I like to find a solution how can I include another html template ( individual_sum.html ) into it with the javascript too. individual_sum.html has lots of javascript codes in the file. I don't use external javascript file because I don't know how if there any way to use Django Template Language in js. I have queries in my view that pushes data dynamically so I need DTL.

I included the individual_sum.html into the main.html and the content appears but charts and other javascript stuff are blank.

main.html

{% extends 'stressz/base.html' %}
{% block body%}

content of main.html

{% include 'stressz/individual_sum.html' %}
{% endblock %}

Is there any solution to have fully (with javascript) working individual_sum.html in main.html ? Or can I use external javascript file that has Django's for and if/elif statements working in it? My aim is to have smaller separated files and not one giant.

Yes you can use the javascript externally with Django. You can create a separate file say myscript.js and then add it as a static file to your django project. Then link it in the base file of your templates like <script src="{% static 'myscript.js' %}"></script> . It will work with every html file that extends from the base file.

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