开发者

Add custom js to django form

I know how to do this:

class CalendarWidget(forms.TextInput):
    class Media:
        js = ('animations.js', 'actions.js')

But then i get something like: "<script type="text/javascript" src="http://media.example.com/animations.js">"

What I want is something like this:

<script>callMYFunction(sdf); </script>

By doing s开发者_如何转开发omething like this:

class CalendarWidget(forms.TextInput):
    class Media:
        js = (callMYFunction(sdf),)

But I can't get this do work.. Any ideas?


You'll have to manually add that to the render method of your Widget.

class CalendarWidget(forms.TextInput):
    def render(self, name, value, attrs=None):
        out = super(CalendarWidget,self).render(name, value, attrs=attrs)
        return out + '<script type="text/javascript">callMyFunction(sdf)</script>'

    class Media:
        js = ('animations.js', 'actions.js') # callMyFunction should be defined in one of these
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜