开发者

Add custom js to django form

开发者 https://www.devze.com 2023-01-10 10:51 出处:网络
I know how to do this: class CalendarWidget(forms.TextInput): class Media: js = (\'animations.js\', \'actions.js\')

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

精彩评论

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