Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 something like this:

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

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

like image 539
Freddy Avatar asked Dec 14 '25 20:12

Freddy


1 Answers

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
like image 54
Wogan Avatar answered Dec 17 '25 22:12

Wogan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!