I currently handle this issue in a much easier way:
I've created an app which allows creating a context for Javascript (this
basically works like the template context processors do, calling defined
functions and combining its results into a dict). The resulting context
will be available in the templates as a normal variable (a context
processor puts it there). Inside my base template I define something
like: "var config = {% json js_context %};". This could be moved into
its own template tags, but I kind of like it this way.

This will store my own, extendable configuration into an project
specific variable (javascript object), which I can easily access
(config.foo.bar). It can be used to store URLs, but may also contain
generic things like MEDIA_URL.

So far it only solves one problem, getting variables into JS in an sane,
structured way...it doesn't solve handling URLs which accept parameters.
URL that don't need parameters are easy to, resolve() does all the work.
Parameters get tricky, but I have come up with an easy and efficient
solution, although it does look kind of strange. I use resolve() to get
a valid URL, but put some placeholder into it, which means:
resolve('my_named_view', kwargs={'slug': '__slug__'})

On the JS-side creating the final url just needs a simple string
replacement. Of course the URL needs to be valid, which means it may not
fit all situations (numeric ids: '(?P<pk>[0-9]+|__pk__)'?). Anyways this
works fine for me, while still being extenable enough. In addition it
solves the more generic problem of putting variables into JS, which is
kind of nice I think.

The resulting JS could be cached using normal template caching tags, or
may be moved into its own HTTP request if wanted. The idea of using some
kind of context processors for JS-context is what this email is
about. ;-)

David




-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to