Is it possible to use django's custom template tags to insert code in other blocks in the template?

跟風遠走 提交于 2019-12-23 02:42:05

问题


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.

This component requires the inclusion of

    <script type="text/javascript" src="../myscript.js"></script>

at the bottom of the page and

    <link rel="stylesheet" type="text/css" href="../mystyle.css">

at the top.

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 I put in:

    { % mytag %} <div>editable element</div> { % endmytag %}

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)


回答1:


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.



来源:https://stackoverflow.com/questions/13624487/is-it-possible-to-use-djangos-custom-template-tags-to-insert-code-in-other-bloc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!