How to manage Javascript modules in django templates?

Posted by John Mee on Stack Overflow See other posts from Stack Overflow or by John Mee
Published on 2009-11-18T02:43:12Z Indexed on 2010/03/09 3:21 UTC
Read the original article Hit count: 623

Filed under:

Lets say we want a library of javascript-based pieces of functionality (I'm thinking jquery): For example:

  1. an ajax dialog
  2. a date picker
  3. a form validator
  4. a sliding menu bar
  5. an accordian thingy

There are four pieces of code for each: some Python, CSS, JS, & HTML.

What is the best way to arrange all these pieces so that:

  • each javascript 'module' can be neatly reused by different views
  • the four bits of code that make up the completed function stay together
  • the css/js/html parts appear in their correct places in the response
  • common dependencies between modules are not repeated (eg: a javascript file in common)

x--------------

It would be nice if, or is there some way to ensure that, when called from a templatetag, the templates respected the {% block %} directives. Thus one could create a single template with a block each for CSS, HTML, and JS, in a single file. Invoke that via a templatetag which is called from the template of whichever view wants it. That make any sense. Can that be done some way already? My templatetag templates seem to ignore the {% block %} directives.

x--------------

There's some very relevant gasbagging about putting such media in forms here http://docs.djangoproject.com/en/dev/topics/forms/media/ which probably apply to the form validator and date picker examples.

© Stack Overflow or respective owner

Related posts about django