问题描述
我正在编写一个自定义小部件,它需要一些我需要输出的支持 JavaScript 代码.
I'm writing a custom widget that requires some supporting javascript code that I need output somehwere.
选项有:
- 在 html 代码之后直接转储它.
- 将其附加到表单的媒体中.
- 将其附加到全局
onReady
部分.
我的直觉是避免以下事情:
My gut instinct is to avoid things like:
<!-- original widget output -->
<input id="date" />
<-- Appended javascript -->
<script type="text/javascript">
jQuery('#date').datepicker()
</script>
相反,我最近在我的 PHP 项目中选择了第 3) 项.Django有做2或3的好方法吗?我希望我可以从小部件的 render
函数的上下文中利用这种方法.如果我的小部件不知道它所在的表单,这可能会排除选项 2).
Instead, I've opted for item 3) most recently in my PHP projects. Does Django have a nice way of doing 2 or 3? I'm hoping that I can utilize this methodology from the context of my widget's render
function. This may preclude option 2) if my widget doesn't have a any idea of the form it's on.
推荐答案
看看form media http://docs.djangoproject.com/en/dev/topics/forms/media/#topics-forms-media
Take a look at form media http://docs.djangoproject.com/en/dev/topics/forms/media/#topics-forms-media
这篇关于Django:将 Javascript 添加到自定义小部件的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!