是否可以使用django的自定义模板标签在模板的其他块中插入代码?
我正在编写一个自定义模板标签,该标签将HTML元素与一些代码包装在一起以使其可编辑。这由一些CSS和JS备份,JS负责将数据发送到服务器以保存数据。
I'm writing a custom template tag that wraps an HTML element with some code to make it editable. This is backed up by some CSS, and JS that takes care of sending the data to the server to save it.
此组件需要包含
<script type="text/javascript" src="../myscript.js"></script>
位于页面底部,
<link rel="stylesheet" type="text/css" href="../mystyle.css">
在顶部。
我已经页面模板中有两个 js和 css模板块。
I already have two "js" and "css" template blocks in the page template.
我的问题-如果我的标签存在,自定义模板标签是否可以在相关页面块中一次包含这些脚本?
My question - is there a way for for the custom template tag to include these scripts once in the relevant page blocks, if my tag is present?
IE
{ % mytag %} <div>editable element</div> { % endmytag %}
相应的 js和 css模板块已填充script\link标记?
and the "js" and "css" template blocks gets populated with the relevant script\link tags?
(我不想在每个页面中都包含这些标记,因为只有部分页面会使用此标记)
(I don't want to preinclude those in every page, as only some of my pages will use this tag)
您可以尝试使用 Django -Sekizai 从您的自定义标记呈现的模板中开始,以将CSS和JS文件添加到另一个块中,这是Sekizai设计的目的。我在基于Django-CMS的项目中广泛使用了它,但是我没有尝试使用您的用例。
You might try using Django-Sekizai from within the template your custom tag renders to add CSS and JS files to another block, which is what Sekizai is designed to do. I use it extensively with Django-CMS based projects, but I haven't tried your use case.