tags:

views:

242

answers:

1

I'm writing a custom widget that requires some supporting javascript code that I need output somehwere.

The options are:

  1. Dump it right after the html code.
  2. Append it to the form's media.
  3. Append it to a global onReady section.

My gut instinct is to avoid things like:

<!-- original widget output -->
<input id="date" />
<-- Appended javascript -->
<script type="text/javascript">
jQuery('#date').datepicker()
</script> 

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.

+2  A: 

Take a look at form media http://docs.djangoproject.com/en/dev/topics/forms/media/#topics-forms-media

mountainswhim
Duh. Well that was obvious ;)The one thing about this is that it's capable of including files, but doesn't seem to have the ability to append a unique script chunk alongside it.I could change my approach and use class based selectors to attach, in this case, jQuery datepickers.
Koobz