admin管理员组文章数量:1334409
i have included a js file(eg:application.js) inside my html page but i couldn't able to use django template tag inside that js file ,is there any way to use django template tag directly inside external js file?
i have included a js file(eg:application.js) inside my html page but i couldn't able to use django template tag inside that js file ,is there any way to use django template tag directly inside external js file?
Share asked Feb 18, 2012 at 7:41 user1118818user1118818 593 silver badges6 bronze badges 03 Answers
Reset to default 6Only if you will parse JS like a template.
If you need some variable inside JS (like STATIC_URL
), it is ok to use
<script>window.staticUrl = "{{ STATIC_URL }}"</script>
or
<html data-static-url="{{ STATIC_URL }}">
in your base template
No, not if you include the application.js file as
<script src="application.js"></script>
The template loader form Django will just render this to the final output of the html code. The Django template tags/filters only render on template pages - they do not apply to external sources.
You could attempt to load the JavaScript file as text into the template. So instead of referencing an external file, you would basically "paste" the code from the file into the template.
You can serve your javascript as dynamic page. Create new django view to serve it (or use django.views.simple.direct_to_template) and use template with your actual javascript content.
本文标签: using django template tags inside external JavaScriptStack Overflow
版权声明:本文标题:using django template tags inside external JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742263665a2442983.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论