最佳实践,包括django模板中的javascript [英] best practice including javascript in django template
问题描述
以前,我以前在模板中这样做了
Previously I used to do like this in a template
<html>
...
<script>
{% include "myapp/includes/jquery-1.7.1.min.js" %}
{% include "myapp/includes/myscript.js" %}
</script>
...
但是这会导致所有的js代码显示在页面源
But this causes all the js code to be shown on the page source.
我没有在我的模板中使用任何表单,所以我可以使用Media类添加js吗?
I am not using any Form in my template, so can I use Media class for adding js?
我应该使用< script src =..
或链接ref =..来添加javascript文件?哪个是更好的方法?
Should I just use <script src=".."
or link ref=".." for adding javascript files? Which is the better way?
推荐答案
使用< script src =yourscript.js> < / script>
像往常一样,没有包含
模板标签。
Use <script src="yourscript.js"></script>
as usual, without the include
template tag.
Django 包含
模板标签并不意味着加载JavaScript源代码。它用于包含一个子模板,其标记可以访问包含模板的模板上下文。请阅读此处。
Django include
template tag is not meant to load JavaScript source. It is used to include a sub template whose markup can access the template context of the including template. Read here.
这篇关于最佳实践,包括django模板中的javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!